Java 如何解决 ElementNotInteractableException: Element 在 Selenium webdriver 中不可见?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43868009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to resolve ElementNotInteractableException: Element is not visible in Selenium webdriver?
提问by Aarthi
回答by NarendraR
Actually the Exception is Element Not Visible
实际上例外是 Element Not Visible
The best practice is to user Implicit wait
below driver Instantiation so it get sufficient time fine element before through the exception
最佳实践是Implicit wait
在驱动程序实例化下方的用户,以便在通过异常之前获得足够的时间细元素
driver.get("http://www.testsite.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Still facing issue as some element require more time you have to user ExplicitWait
for individual element to satisfy certain condition
仍然面临问题,因为某些元素需要更多时间,您必须使用ExplicitWait
单个元素才能满足某些条件
In your case you are facing element not visible exception
then use wait condition in following way
在您的情况下,您正面临元素,not visible exception
然后按以下方式使用等待条件
WebDriverWait wait = new WebDriverWait(driver, 120);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.your_Elemetnt));
回答by DebanjanB
ElementNotInteractableException
元素不可交互异常
ElementNotInteractableException is the W3C exception which is thrown to indicate that although an element is present on the HTML DOM, it is not in a state that can be interacted with.
ElementNotInteractableException 是 W3C 异常,它被抛出以表明虽然HTML DOM上存在元素,但它并不处于可以与之交互的状态。
Reasons & Solutions :
原因和解决方案:
The reason for ElementNotInteractableExceptionto occur can be numerous.
ElementNotInteractableException发生的原因有很多。
Temporary Overlay of other
WebElement
over theWebElement
of our interest:In this case, the direct solution would have been to induce
ExplicitWait
i.e.WebDriverWait
in combination withExpectedCondition
asinvisibilityOfElementLocated
as 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();
A better solution will be to get a bit more granular and instead of using
ExpectedCondition
asinvisibilityOfElementLocated
we can useExpectedCondition
aselementToBeClickable
as follows:WebDriverWait wait1 = new WebDriverWait(driver, 10); WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_element_to_be_clicked"))); element1.click();
Permanent Overlay of other
WebElement
over theWebElement
of our interest:If the overlay is a permanent one in this case we have to cast the
WebDriver
instance asJavascriptExecutor
and perform the click operation as follows:WebElement ele = driver.findElement(By.xpath("element_xpath")); JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click();", ele);
其他
WebElement
超过WebElement
我们利益的临时叠加:在这种情况下,直接的解决方案是将
ExplicitWait
ieWebDriverWait
与ExpectedCondition
如下组合invisibilityOfElementLocated
: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
实例转换为JavascriptExecutor
并执行点击操作,如下所示:WebElement ele = driver.findElement(By.xpath("element_xpath")); JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click();", ele);
回答by tutug
A solution to this for Javascript looks like this. You will have to modify the time to suit your need.
Javascript 的解决方案如下所示。您将不得不修改时间以满足您的需要。
driver.manage().setTimeouts({ implicit: 30000 });
driver.manage().setTimeouts({ implicit: 30000 });
Hope this is helpful to someone. see the docsfor reference
希望这对某人有帮助。请参阅文档以供参考
回答by ProgrammingRookie
I got this because the element I wanted to interact with was covered by another element. In my case it was an opaque overlay to make everything r/o.
我得到这个是因为我想与之交互的元素被另一个元素覆盖。在我的情况下,它是一个不透明的覆盖层,可以使所有东西都变成 r/o。
When trying to click an element UNDER another element we usualy get "... other Element would receive the click " but not always :.(
当试图点击另一个元素下的一个元素时,我们通常会得到“......其他元素会收到点击”但并不总是:.(