无法点击按钮; - “元素不可交互”

时间:2021-07-12 23:56:40

标签: java eclipse selenium script

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

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

website

code

2 个答案:

答案 0 :(得分:0)

我明白了,有一个接受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");

答案 1 :(得分:0)

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

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

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