【问题标题】:Can not click on a button; - "element is not interactable"无法点击按钮; - “元素不可交互”
【发布时间】:2021-09-22 01:41:43
【问题描述】:

我有一个脚本,我试图在电子商务网站上按下一个按钮,但是当我运行一个脚本时,我得到了"Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable "

当我将 xpath 放入 chropath 时,元素已定位,所以我猜路径 driver.findElement(By.xpath("//form/fieldset[2]/button")).click(); 是正确的。请查看随附的屏幕截图并保持温和我是编程新手和这个网站:/

【问题讨论】:

  • 您应该使用等待您的网络驱动程序来执行此操作,您可以使用 sleep 进行显式等待或使用特殊的直到可用命令。另外我会检查以确保您拥有正确的元素
  • 您应该更新问题,添加 url 和您的代码,其他人可以使用它来帮助您。图片不是显示代码的正确方式。
  • @Alex 不要发布图片。见meta.stackoverflow.com/questions/285551/…

标签: java eclipse selenium


【解决方案1】:

我明白了,有一个接受 cookies 按钮,如果你想点击它,你可以使用下面的代码:-

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#onetrust-accept-btn-handler"))).click();

然后像这样点击Sleeve length

wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//form/fieldset[2]/button"))).click();

请注意,在与任何网络元素交互之前,您应该全屏打开浏览器。

driver.manage().window().maximize();
driver.get("Your URL");

【讨论】:

  • 非常感谢!!!问题是窗口没有最大化。
【解决方案2】:

您可以使用 JavascriptExecutor 来触发点击:

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", yourWebElement);

但我也建议在 Thread.sleep 中使用显式/隐式等待(我已链接 article,您可以在 google 上找到更多信息)。

【讨论】:

    猜你喜欢
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 2016-08-01
    • 2017-10-15
    • 1970-01-01
    • 2019-10-19
    相关资源
    最近更新 更多