-1
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"
Guy
  • 46,488
  • 10
  • 44
  • 88

3 Answers3

1

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")
Guy
  • 46,488
  • 10
  • 44
  • 88
  • How can you be sure `elem[0]` contains the desired element you are looking out for? There can be multiple elements identified through `find_elements_by_id("msgButtonAffirm")` and the desired element can be `elem[2]`, `elem[3]` as well. – undetected Selenium Nov 26 '19 at 09:59
  • @DebanjanB `elem[0]` will contain the same element `find_element_by_id` will return. – Guy Nov 26 '19 at 10:01
  • Exactly, that's what I was saying, so effectively, using `elem[0]` doesn't have any advantage over OP's `find_element_by_id()` – undetected Selenium Nov 26 '19 at 10:03
  • @DebanjanB The advantage is `find_elements_by_id` and `elem[0]` won't throw `NoSuchElementException`, which the OP receive if the popup doesn't appear, `find_element_by_id` will. – Guy Nov 26 '19 at 10:05
  • Only handling `NoSuchElementException` wouldn't solve the purpose (read as the usecase), rather I think we need the relevant HTML so the element can be uniquely identified first inducing WebDriverWait (if needed) and then proceed. – undetected Selenium Nov 26 '19 at 10:09
  • @DebanjanB I did read the question, the popup is not always there. Using `WebDriverWait` is a bad idea in that case. – Guy Nov 26 '19 at 10:10
0

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")
0

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • And what if the popup will never appear? it will wait for 20 seconds for no reason. – Guy Nov 26 '19 at 11:15
  • @Guy The _timespan_ of **20** seconds is just an example here and can be set to any value e.g. **3**, **5**, **7**, **10**, etc. Effectively, the _timespan_ should be configured as per the _Test Plan_. The _Test Plan_ should define the _timeout_ duration for _Modal Dialog Box_, _Angular_ elements, _JavaScript_ based elements, etc. – undetected Selenium Nov 26 '19 at 11:21
  • The point is not how much time it waits, the point is it waits needlessly for a popup that might not be there. – Guy Nov 26 '19 at 11:24