Home > database >  Selenium Python: Element is not clickable
Selenium Python: Element is not clickable

Time:07-28

I am testing a webapp created with python dash using selenium. I am trying to click a tab but always get the ElementClickIntercepted Exception.

Note: Of course I stumbled over similiar problems but most times the problem is that the element cannot be clicked as is did not load yet - I already built in waits! - or the fact that another element would receive the click which cannot be the case either. Note that the first item is always preselected and i want to choose the second.

#Selct X-Axis
x_axis = driver.find_element(By.XPATH, "//*[@id='navbar']/a[2]")
x_axis.click()
driver.implicitly_wait(2)
try:
    WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, "//a[text()='Even']"))  
)
except:
    raise Exception
even= driver.find_element(By.XPATH, "//a[text()='Even']")
even.click()

Exception snapshot:

The Exception

HTML snapshot:

The html-code

CodePudding user response:

Based on what you've posted, By.XPATH, "//a[text()='Even']" is probably selecting an HTML element different from what you're expecting.

There appears to be an <a> with text that starts with 'Even', but you've shown nothing with text that equals 'Even'.

BTW, when asking a question, you ought to at least paste the relevant HTML as text, rather than as a screenshot.

CodePudding user response:

Use contains(text(), 'even') instead of text()='even' in your XPath. Because, text()='even' means the text must be 'even' but not contain 'even', and in your HTML code, the text seems not only have 'even'.

CodePudding user response:

To click on either of the element instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • $$$ Response:

    • Using PARTIAL_LINK_TEXT:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Response"))).click()
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(@id, 'content')]//ul//div[@class='nav-item']//a[contains(., 'Response')]"))).click()
      
  • Even $$$:

    • Using PARTIAL_LINK_TEXT:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Even"))).click()
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(@id, 'content')]//ul//div[@class='nav-item']//a[starts-with(., 'Even')]"))).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
    
  • Related