Search code examples
pythonseleniumselenium-webdriverxpathwebdriverwait

Selecting element with respective to the description text using Selenium and Python


I'm trying to use the following code to find and click where it says OAT (ADS05....) in the included screenshot:

oatObj = driver.find_element(By.XPATH,"//span[contains(@class, 'treeImg') and contains(., 'OAT ')]") 
oatObj.click()

enter image description here

This is popping up the following error:

ElementNotInteractableException: Message: element not interactable

This element is definitely clickable, and other places in the code I'm using a similar XPATH match which do work. Is there something different about this XPATH that anyone can see? Here's a screenshot of the element and inspect pane when clicked:

enter image description here


Solution

  • The innerText does contains the text OAT but canonically the text starts-with the text OAT


    Solution

    To click on the clickable element ideally you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategy:

    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li/span[contains(@class, 'treeImg') and starts-with(., 'OAT')]"))).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