Home > Net >  How to avoid error if element is not clickable at point (selenium python)?
How to avoid error if element is not clickable at point (selenium python)?

Time:08-21

I have this script that I wrote with selenium python, but sometimes I got this problem at this one place, when it shows that is not clickable. Element is there and it can be found but it can not be clickable, because sometimes I need to meet required conditions. See error bellow.

selenium.common.exceptions.ElementClickInterceptedException: Message: Element <div > is not clickable at point (242,476) because another element <div > obscures it

But when this happens I got error and script is stopped. So I need to start it again. Is it possible to do something like this (i did not found the way)...

if not clickable...at driver('selected class')
    driver.refresh()
else
    pass

This is my thinking, I was unable to find the way but maybe there is a special name or something. If you have any idea, any help is more then welcome.

Thanks in advance!

CodePudding user response:

To click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you wrap up the line of code within a try-except{} block and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    while True:
      try:
          WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class$='placeholder']"))).click()
          break
      except TimeoutException:
          driver.refresh()
          continue
    
  • Using XPATH:

    while True:
      try:
          WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, 'placeholder')]"))).click()
          break
      except TimeoutException:
          driver.refresh()
          continue
    
  • 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
    
  • Related