0

(Python)I am trying to catch an exception while using Selenium chrome Driver, however, I have so far been unable to catch the exception and prompt the "Failed at this click!" print statement. Any advice on how to go about this issue (The original issue is with the .click() as I am having trouble with consistency but I think catching the exception at this point would be enough to get by).

Exception below :

File "/Users/J*****i/anaconda/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)

WebDriverException: unknown error: Element is not clickable at point (145, 1818)
  (Session info: chrome=64.0.3282.186)
  (Driver info: chromedriver=2.31.488774 (7e15618d1bf16df8bf0ecf2914ed1964a387ba0b),platform=Mac OS X 10.10.5 x86_64)

Code below :

    URL2 = URL_in_2
    driver = webdriver.Chrome('/Users/J******i/Desktop/chromedriver')
    driver.get(URL2)
    cookie_disclaimer = driver.find_element_by_xpath("//*[contains(text(), 'OK')]")
    cookie_disclaimer.click()
    try:
        element = WebDriverWait(driver,10).until(
                EC.presence_of_element_located((By.ID, "sub-navigation"))
        )
    except:
        "Failed at this click!"
    finally:
        print("[D2] successful first click: " + str(element) + " found")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Jack L
  • 5
  • 2
  • 1
    place .click() method in try-except block – ramazan polat Feb 27 '18 at 22:52
  • Possible duplicate of [Selenium Web Driver & Java. Element is not clickable at point (36, 72). Other element would receive the click:](https://stackoverflow.com/questions/44912203/selenium-web-driver-java-element-is-not-clickable-at-point-36-72-other-el) – undetected Selenium Feb 28 '18 at 01:28

1 Answers1

0

To capture the exception for click(), keep it in try catch block. Modify your code as below:

    URL2 = URL_in_2
    driver = webdriver.Chrome('/Users/J******i/Desktop/chromedriver')
    driver.get(URL2)
    try:
        cookie_disclaimer = driver.find_element_by_xpath("//*[contains(text(), 'OK')]")
        cookie_disclaimer.click()
        element = WebDriverWait(driver,10).until(
        EC.presence_of_element_located((By.ID, "sub-navigation"))
    )
    except:
        "Failed at this click!"
    finally:
        print("[D2] successful first click: " + str(element) + " found")

OR

Not sure why you have implicit wait for element presence after click() but in your code you are capturing any exception while waiting for presence of an element with id 'sub-navigation'

Dev
  • 36
  • 3