按钮元素不可交互 Selenium

发布于 2025-01-09 04:28:56 字数 1129 浏览 0 评论 0原文

我正在尝试编写一个脚本来使用 Selenium 和 Python 自动执行某些任务,每次尝试单击按钮时

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

driver = webdriver.Chrome(options=options)
xpath = "/html/body/app-root/app-prime/div/mat-sidenav-container//app-detail-component/main//div/span/button[@aria-label='Prenota']"

# Wait for the element to be visible, always true
WebDriverWait(driver, 5).until(expected_conditions.presence_of_element_located((By.XPATH, xpath)))
# Try to click on element, get an error
driver.find_element(By.XPATH, xpath).click()

都会收到以下错误,

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

我确信该元素已正确定位并且必须匹配,但它应该仅对第一个进行操作。

我尝试过:

  • 尝试单击子标签,例如其他div和span
  • 等待它可单击
  • 等待隐式等待

这些活动都没有成功

编辑:显然我的机器上存在olly问题

I'm trying to write a script to automate some tasks with Selenium and Python, and every time I try to click on a button

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

driver = webdriver.Chrome(options=options)
xpath = "/html/body/app-root/app-prime/div/mat-sidenav-container//app-detail-component/main//div/span/button[@aria-label='Prenota']"

# Wait for the element to be visible, always true
WebDriverWait(driver, 5).until(expected_conditions.presence_of_element_located((By.XPATH, xpath)))
# Try to click on element, get an error
driver.find_element(By.XPATH, xpath).click()

I get the following error

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

I know for sure that the element gets located correctly and has to match, but it should act only on the first one.

I tried:

  • Trying to click child tags, such as others divs and span
  • Waiting for it to be clickable
  • Waiting with an implicit wait

None of those activities were successful

Edit: Apparently the issue olly exist on my machine

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

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

发布评论

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

评论(2

烟燃烟灭 2025-01-16 04:28:56

由于多种原因,它可能无法点击。您可能想检查页面上是否有某些元素位于顶部,以便该元素当时不可交互,例如某些弹出窗口/iframe 等。当时可能还有其他一些元素会收到点击。

您可以尝试单击操作 - 像这样

myElement = driver.find_element_by_xpath("myXpath")
webdriver.ActionChains(driver).move_to_element(myElement).click(myElement).perform()

It could be not clickable for a number of reasons. You might want to check if there is some element on the page layered on top so that that element is not interactable at that time, e.g some popup/iframe etc. There could be some other element that will receive click at that time.

You could try an actions click - something like this

myElement = driver.find_element_by_xpath("myXpath")
webdriver.ActionChains(driver).move_to_element(myElement).click(myElement).perform()
掌心的温暖 2025-01-16 04:28:56

其中之一应该有效。

IJavaScriptExecutor executor = (IJavaScriptExecutor)WebDriver.Driver;
            executor.ExecuteScript("arguments[0].click();", webElement);

Actions actions = new Actions(WebDriver.Driver);
            actions.MoveToElement(webElement).Click().Perform();

注意 - 这些是 C 锐代码。尝试在java中做同样的事情。

One of this should work.

IJavaScriptExecutor executor = (IJavaScriptExecutor)WebDriver.Driver;
            executor.ExecuteScript("arguments[0].click();", webElement);

Actions actions = new Actions(WebDriver.Driver);
            actions.MoveToElement(webElement).Click().Perform();

Note - these are c sharp code. try to do the same in java.

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