使用Selenium和Python选择有关描述文本的元素

发布于 2025-01-23 19:55:16 字数 734 浏览 0 评论 0原文

我正在尝试使用以下代码查找并单击包含的屏幕截图中的燕麦(ADS05 ....)的位置:

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

“在此处输入映像”

这是弹出以下错误:

ElementNotInteractableException: Message: element not interactable

绝对可以单击此元素,以及我正在使用类似XPath匹配的代码中的其他位置。这个XPath有什么不同的东西吗?这是元素的屏幕截图,然后单击时检查窗格:

”在此处输入图像说明”

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落叶缤纷 2025-01-30 19:55:16

Innertext确实包含文本 燕麦 ,但在规范上,文本 start with-with with text 燕麦


解决方案

要单击 单击 元素理想情况下您需要诱导 WebDriverWait 对于以下 定位器策略

  • 使用 xpath :< /p>

      webDriverWait(驱动程序,20).until(ec.element_to_be_be_clickable(((by.xpath,“ // li/span),li/span [contains(@class,'treeimg')和starting-with(。,'oat'' )]“)))。点​​击()
     
  • 注意:您必须添加以下导入:

     来自selenium.webdriver.support.ui导入webdriverwait
    从selenium.webdriver.common.通过进口
    从selenium.webdriver.support进口预期_conditions作为ec
     

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
    
蓝眼泪 2025-01-30 19:55:16

尝试在单击元素之前先

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

使用

等待

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

勿挽旧人 2025-01-30 19:55:16

element notintactableException:消息:元素不相互作用您遇到的错误,因为该元素在访问它的位置无法交互。可能是各种原因,有时不可见并尝试单击,有时会与其他元素覆盖,有时是视口。

请找到以下解决方案。

#1使用WebDriverWait()

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

#2使用Action 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使用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)

请在库下导入:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文