I am building an automation script which will open the browser and login to a portal. It has to click a few buttons and pages. I am using selenium in python, so for example to click a button I am using WebDriverWait:
BTN= (By.XPATH, '''//a[#ui-sref="app.colleges.dashboard({fid: app.AppState.College.id || Colleges[0].id, layout: app.AppState.College.layout || 'grid' })"]//div[#class='item-inner']''')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(BTN)).click()
Is there any return code or any response code I can get from WebDriverWait so that in the script I am sure that it runs successfully and I can proceed ahead
Your code trial is pretty perfect which is as follows:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(BTN)).click()
The expected_conditions method element_to_be_clickable() returns the WebElement when it is is visible and enabled so you can directly invoke click() method on it.
Now as per your comment update if you want to implement 3-4 retry attempts to click on the element you can use the following solution:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
BTN= (By.XPATH, '''//a[#ui-sref="app.colleges.dashboard({fid: app.AppState.College.id || Colleges[0].id, layout: app.AppState.College.layout || 'grid' })"]//div[#class='item-inner']''')
for i in range(3):
try:
WebDriverWait(driver, 5).until(EC.element_to_be_clickable(BTN)).click()
print("Element was clicked")
break
except TimeoutException:
print("Timeout waiting for element")
driver.quit()
Related
I am trying to use selenium to click certain buttons within the bank of america simulator, but the buttons don't seem to ever click. No new link is reached, which is something I haven't encountered before.
https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/
I want to click "Sign in options" and then click "Sign in: Recognized device"
I tried using selenium to click the button and I get no error. Nothing happens at all and the program continues, so I know it's not an issue with not finding the button. My current code is as follows:
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/')
sleep(3)
login_button = driver.find_element("id", "landing_sign")
driver.execute_script("arguments[0].click();", login_button);
This code worked fine for me.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/')
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "landing_sign")).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[aria-labelledby='signInOpt3']")).click()
NOTE: Using sleep is a bad practice, use WebDriverWait and wait for the specific state you need instead.
To click on the clickable elements you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategies:
Code block:
driver.get('https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#landing_sign"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='/onlinebanking_demo/OLB_Simulator/SignIn/recognized']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
I want to go from this page to this https://resultats.ffbb.com/organisation/b5e6211d5970.html to this page https://resultats.ffbb.com/championnat/b5e6211f621a.html?r=200000002810394&d=200000002911791&p=2 by clicking on 'Régional féminin U15'.
I have tried many solutions but the best I had, is not working systematically.
Please coul you help me?
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import selenium.webdriver.support.ui as ui
import time
driver = webdriver.Firefox()
driver.get("https://resultats.ffbb.com/organisation/b5e6211d5970.html")
driver.switch_to.frame("idIframeChampionnat")
#sign_in = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/pre/span[93]'))).click();
button = driver.find_element_by_partial_link_text(u"minin U15")
button.click()```
To click() on the link with text as Régional féminin U15 as the elements are within an iframe so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://resultats.ffbb.com/organisation/b5e6211d5970.html")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#idIframeChampionnat")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Régional féminin U15"))).click()
Using XPATH:
driver.get("https://resultats.ffbb.com/organisation/b5e6211d5970.html")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='idIframeChampionnat']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Régional féminin U15"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
Reference
You can find a couple of relevant discussions in:
Switch to an iframe through Selenium and python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
Try
driver.switch_to.frame("idIframeChampionnat")
button = driver.find_element_by_xpath("//a[contains(text(), 'minin U15')]")
button.click()
or
driver.switch_to.frame("idIframeChampionnat")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(), 'minin U15')]"))).click()
This is my code example in groovy (sorry, haven't python env), pretty similar to yours, and it works perfectly 20 times in a loop even without timeouts.
#Test(invocationCount = 20)
void test() {
driver.get('https://resultats.ffbb.com/organisation/b5e6211d5970.html')
driver.switchTo().frame(
driver.findElement(By.xpath("//iframe[#id='idIframeChampionnat']"))
)
driver.findElement(By.xpath("//a[contains(text(), 'minin U15')]")).click()
driver.switchTo().defaultContent()
assert driver.getCurrentUrl() ==
'https://resultats.ffbb.com/championnat/b5e6211f621a.html?r=200000002810394&d=200000002911791&p=2'
}
So, I have no ideas. Maybe just add driver.switch_to.default_content() after click?
I know, there are plenty of tutorials out there for this task. But it seems like I'm doing something wrong, I need help by telling me how to press it, doesn't need to be by text.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.LINK_TEXT, 'login or register'))
)
element.click()
except:
print('exception occured')
driver.quit()
I'm trying to press the "login or register" button from the site wilds.io, but it seems like it can't find that button after 10 seconds. I'm probably accessing the button in a wrong way.
To click on the element with text as login or register you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "login or register"))).click()
Using PARTIAL_LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "login or register"))).click()
Using XPATH using text():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[text()='login or register']"))).click()
Using XPATH using contains():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[contains(., 'login or register')]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I want to close the popup window that appears when I hit a particular url. Here is the "inspect element" window of that page:
Here is what I have tried:
driver.find_element_by_css_selector("i[#class='popupCloseIcon']").click()
But it gives following error:
InvalidSelectorException: Message: Given css selector expression
"i[#class='popupCloseIcon']" is invalid: InvalidSelectorError:
Document.querySelector: 'i[#class='popupCloseIcon']' is not a valid
selector: "i[#class='popupCloseIcon']"
Here is the url where popup appears: https://www.investing.com/equities/oil---gas-dev-historical-data
Once the url is opened through selenium, the pop up appears after a few seconds.
How can I click that close button?
The popup appears after some time, so you need wait to solve this. And you have invalid selector : i[#class='popupCloseIcon'], please use i[class*='largeBannerCloser']
Try the below:
driver.get('https://www.investing.com/equities/oil---gas-dev-historical-data')
try:
popup = WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "i[class*='largeBannerCloser']")))
popup.click()
except TimeoutException as to:
print(to)
This is wait until 60 seconds maximum.
Following import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
As the pop up appears after a few seconds accessing the url https://www.investing.com/equities/oil---gas-dev-historical-data to to close the popup window you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "i.popupCloseIcon.largeBannerCloser"))).click()
Using XPATH:
WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.XPATH, "//i[#class='popupCloseIcon largeBannerCloser']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
First, "i[#class='popupCloseIcon']" is a invalid css selector locator, it should be "i[class='popupCloseIcon']". Second, there are four elements mapped to the "i[class='popupCloseIcon']", the css selector "div.right>i.popupCloseIcon" will help you to locate the target element
I need a script which logs me in to github and presses a Read the guide button. I've got it to log in but after it's done it does not work any more. Please help.
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://github.com/session')
login_area = browser.find_element_by_name('login')
login_area.send_keys('maximmashkov')
login_area = browser.find_element_by_id('password')
login_area.send_keys('12345')
submit_button = browser.find_element_by_name('commit')
submit_button.click()
To click() on the element with text as Read the guide you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using LINK_TEXT:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Read the guide"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-primary[href='https://guides.github.com/activities/hello-world/']"))).click()
Using XPATH:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#href='https://guides.github.com/activities/hello-world/' and text()='Read the guide']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Most probably your script fails to wait for the "Read the guide" button to appear, consider using Explicit Wait so Selenium could wait for a defined period of time for the element to appear in DOM
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#initialize your browser here
login_area = browser.find_element_by_name('login')
login_area.send_keys('maximmashkov')
login_area = browser.find_element_by_id('password')
login_area.send_keys('12345')
submit_button = browser.find_element_by_name('commit')
submit_button.click()
submit_button = browser.find_element_by_name('commit')
submit_button.click()
read_the_guide = element = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.LINK_TEXT, "Read the guide")))
read_the_guide.click()
browser.quit()
More information:
How to use Selenium to test web applications using AJAX technology
How to wait for elements in Python Selenium WebDriver
The solution was posted in the question and moved here:
You only have to put a little time.sleep() delay. One second is enough