0

Problem: Having a bit of trouble figuring out how to implement the WebDriverWait method in Python Selenium.

I want to do nothing on NoSuchElementExceptions but use WebDriverWait on StaleElementReference, ElementNotInteractable, ElementClickIntercepted, and whatever ever new one pops up.

The below has worked for me on just StaleElementReference but what do I do If I want to incorporate all similar exceptions besides NoSuchElement? I beleive there's more than just this 3 that could pop up.

def click(selector):
    try:
        elem = browser.find_element_by_css_selector(selector)
        elem.click()      
    except NoSuchElementException:
        pass
    except StaleElementReferenceException:
        elem = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, selector)))
        elem.click()

EDIT: Added Error Message from Solution from @BapRx

    elem.click()
    self._execute(Command.CLICK_ELEMENT)
    return self._parent.execute(command, params)
    self.error_handler.check_response(response)
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <span>...</span> is not clickable at point (730, 523). Other element would receive the click: <div id="JDCol" class="noPad">...</div>
  (Session info: chrome=87.0.4280.88)


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
    click(company_tab_selector) #Company Tab
    elem.click()
    self._execute(Command.CLICK_ELEMENT)
    return self._parent.execute(command, params)
    self.error_handler.check_response(response)
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <span>...</span> is not clickable at point (730, 523). Other element would receive the click: <div id="JDCol" class="noPad">...</div>
  (Session info: chrome=87.0.4280.88)'
2
  • This try syntax for multiple exceptions is as documented here Commented Jan 6, 2021 at 7:22
  • You can group multiple exceptions: except (StaleElementReferenceException, ElementNotInteractableException, ElementClickInterceptedException): Commented Jan 6, 2021 at 7:24

2 Answers 2

1

You could do this:

try:
    elem = browser.find_element_by_css_selector(selector)
    elem.click()      
except NoSuchElementException:
    pass
except Exception as e:
    print(e) # You can still log the error
    elem = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, selector)))
    elem.click()
Sign up to request clarification or add additional context in comments.

2 Comments

so I tried this out and it caught and logged a few stale element exceptions but then brought up this error, any idea what the issue could be? error added to post
for additonal context, the first ElementClickInterceptedException is raised on the first try block and then the same exception is just raised again on the elem = webdriverwait line. It catches all of the stale element references just fine, just has problems with the click intercepted ones.
1

This works for me:

from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait

def wait_for(web_driver, web_element: str, delay=60) -> bool:
try:
    WebDriverWait(web_driver, delay).until(
        ec.presence_of_element_located((By.XPATH, web_element)))
    return True
except TimeoutException as e:
    return False

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.