elem = driver.find_element_by_id("msgButtonAffirm")
if elem.is_displayed():
elem.click()
print("conform popup is avalable and click")
else:
print "Pop-up is not visible"
You can use find_elements_by_id and check if there is anything in the list
elem = driver.find_elements_by_id("msgButtonAffirm")
if elem and elem[0].is_displayed():
elem[0].click()
print("conform popup is avalable and click")
else:
print("Pop-up is not visible")
You can import the exception and work with that as following:
from selenium.common.exceptions import NoSuchElementException
try:
elem = driver.find_element_by_id("msgButtonAffirm")
elem.click()
print("conform popup is avalable and click")
except NoSuchElementException:
print("Pop-up is not visible")
You need to take care of a couple of things:
While clicking on confirmation popups you shouldn't major focus shouldn't be on handling NoSuchElementException
Historically, in majority of the cases confirmation popups resides within modal dialoge boxes so you need to induce WebDriverwait.
The relevant HTML would have helped us to analyze the issue in a better way. However, as per the mentioned points, you need to to induce WebDriverWait for expected_conditions as element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
try:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#msgButtonAffirm"))).click()
print ("Pop-up was clickable and clicked")
except TimeoutException:
print ("Pop-up was not clickable")
Using XPATH:
try:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[#id='msgButtonAffirm']"))).click()
print ("Pop-up was clickable and clicked")
except TimeoutException:
print ("Pop-up was not clickable")
Note : You have to add the following imports :
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Related
How to click the second element if the first element is not clickable using Python Selenium
Code trials:
if self.driver.find_element(By.CSS_SELECTOR, ".estimator-container:nth-child(3) .btn").is_not_clicked():
self.driver.find_element(By.LINK_TEXT, "Dont have the Plate?").click();
To click on any clickable element you need to induce WebDriverWait for the element_to_be_clickable() wrapping up the the code block within a try-except{} block and you can usethe following Locator Strategies:
try:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".estimator-container:nth-child(3) .btn"))).click()
print("First element was clicked")
except TimeoutException:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Dont have the Plate?"))).click()
print("Second element was clicked")
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 am using python selenium chrome driver and i am stuck at this.
How can i loop this code until one of the elements is clickable?
Like if its finally clickable it should get clicked and print ("clickable") and if its still not clickable it should print ("Not Clickable")
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//BUTTON[#type='submit'[text()='Zum Warenkorb hinzufügen']"))).click()
WebDriverWait(driver, 150).until(EC.element_to_be_clickable((By.CLASS_NAME, "c-modal__content")))
I am not sure if your use of uppercase button is correct. Use the same syntax as in html.
One more thing: check your xpath with text():
It should be: //button[#type='submit' and text()='Zum Warenkorb hinzufügen']
Also, the general case for such loop in the case of one element is:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
wait = WebDriverWait(driver, 15)
while True:
try:
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#type='submit' and text()='Zum Warenkorb hinzufügen']")))
print("clickable")
element.click()
except TimeoutException:
break
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'm trying to finding a way to check if a new page/window is opened successfully and has content or not. I know that selenium is not able to check code 200 to see if the page is successfully loaded or not. So what should I do in order to find out if the page is loaded successfully?
while True:
try:
driver.find_element_by_css_selector("#showbtn").click()
print ("Page Loaded Successfully")
break
except:
print ("Page loading failed")
time.sleep(5)
To check for the presence of an element, you can use WebDriverWait with presence_of_element_located like so:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, ...))
)
That will wait until the element is either found or the wait time is reached (10 seconds in the example)
To Click on button Induce WebDriverWait() and element_to_be_clickable() and then click on button.
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#showbtn"))).click()
OR to check pageload successfully you can induce javascript executor before interacting the the element.
WebDriverWait(driver, 20).until(lambda drv: drv.execute_script('return document.readyState == "complete"'))
You need to add following libraries.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Updated code:
while True:
try:
WebDriverWait(driver, 20).until(lambda drv: drv.execute_script('return document.readyState == "complete"'))
print("Page Loaded Successfully")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#showbtn"))).click()
break
except:
print ("Page loading failed")
driver.refresh()
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()