如何解决Selenium webdriver中的ElementNotInteractableException?
在这里,我有我的代码的图像和我的错误的图像。 任何人都可以帮我解决这个问题吗?
尝试使用
Thread.sleep(10000);
使用它时,页面中的Web元素有足够的时间加载
什么是ElementNotInteractableException
?
根据文档, ElementNotInteractableException
是W3Cexception,抛出该exception表示虽然DOM TREE
上存在一个元素,但它不处于可以与之交互的状态。
原因和解决方案:
发生ElementNotInteractableException
的原因可能很多。
-
在我们感兴趣的
WebElement
WebElement
上临时叠加其他WebElement
:在这种情况下,直接解决方案是将
ExplicitWait
即WebDriverWait
与ExpectedCondition
结合使用,因为invisibilityOfElementLocated
为folllows:WebDriverWait wait2 = new WebDriverWait(driver, 10); wait2.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("xpath_of_element_to_be_invisible"))); driver.findElement(By.xpath("xpath_element_to_be_clicked")).click();
一个更好的解决方案是更细化,而不是使用
ExpectedCondition
作为invisibilityOfElementLocated
我们可以使用ExpectedCondition
作为elementToBeClickable
,如下所示:WebDriverWait wait1 = new WebDriverWait(driver, 10); WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_element_to_be_clicked"))); element1.click();
-
通过我们感兴趣的
WebElement
永久覆盖其他WebElement
:如果在这种情况下覆盖是永久覆盖,我们必须将
WebDriver
实例WebDriver
为JavascriptExecutor
并执行单击操作,如下所示:WebElement ele = driver.findElement(By.xpath("element_xpath")); JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click();", ele);
实际上,exception是Element Not Visible
最佳实践是用户Implicit wait
低于驱动程序实例化,以便在通过exception之前获得足够的时间精细元素
driver.get("http://www.testsite.com"); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
仍面临问题,因为某些元素需要更多时间,您必须为单个元素使用ExplicitWait
以满足特定条件
在您的情况下,您面临元素not visible exception
然后以下列方式使用等待条件
WebDriverWait wait = new WebDriverWait(driver, 120); wait.until(ExpectedConditions.visibilityOfElementLocated(By.your_Elemetnt));