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

Time:04-26

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

CodePudding user response:

Try to use wait before click on the element

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH,'YourXpath')))

And also you can use find_elements if there are more elements with same xpath

You can change you way to click by using Java Script and perform Action

CodePudding user response:

ElementNotInteractableException: Message: element not interactable error you are getting because the element is not interactable at the point of accessing it. May be various reason, sometimes it is not visible and trying to click, sometimes overlay with other elements, sometimes for viewport.

Please find following solutions.

#1 using WebDriverWait()

oatObj=WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(@class, 'treeImg') and contains(., 'OAT ')]")))
oatObj.click()

#2 using Actions class

oatObj=WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(@class, 'treeImg') and contains(., 'OAT ')]")))

ActionChains(driver).move_to_element(oatObj).click().perform()

#3 using JavaScripts Executor

oatObj=WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(@class, 'treeImg') and contains(., 'OAT ')]")))

driver.execute_script("arguments[0].click();",oatObj)

Please import below libraries:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

CodePudding user response:

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
    
  • Related