无法点击按钮; - "element is not interactable"

Can not click on a button; - "element is not interactable"

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

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

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

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

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

但我还建议使用 explicit/implicit 等待而不是 Thread.sleep(我链接了一个 article,您可以在 google 上找到更多信息)。