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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:11:34  来源:igfitidea点击:

How to resolve ElementNotInteractableException: Element is not visible in Selenium webdriver?

javaseleniumselenium-webdriverwebdriverwebdriverwait

提问by Aarthi

Here I have the image of my code and the image of my error. Can anyone help me to resolve this issue?

在这里,我有我的代码图像和我的错误图像。谁能帮我解决这个问题?

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by NarendraR

Actually the Exception is Element Not Visible

实际上例外是 Element Not Visible

The best practice is to user Implicit waitbelow 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 ExplicitWaitfor individual element to satisfy certain condition

仍然面临问题,因为某些元素需要更多时间,您必须使用ExplicitWait单个元素才能满足某些条件

In your case you are facing element not visible exceptionthen 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发生的原因有很多。

  1. Temporary Overlay of other WebElementover the WebElementof our interest:

    In this case, the direct solution would have been to induce ExplicitWaiti.e. WebDriverWaitin combination with ExpectedConditionas invisibilityOfElementLocatedas 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 ExpectedConditionas invisibilityOfElementLocatedwe can use ExpectedConditionas elementToBeClickableas follows:

    WebDriverWait wait1 = new WebDriverWait(driver, 10);
    WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_element_to_be_clicked")));
    element1.click();
    
  2. Permanent Overlay of other WebElementover the WebElementof our interest:

    If the overlay is a permanent one in this case we have to cast the WebDriverinstance as JavascriptExecutorand perform the click operation as follows:

    WebElement ele = driver.findElement(By.xpath("element_xpath"));
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", ele);
    
  1. 其他WebElement超过WebElement我们利益的临时叠加

    在这种情况下,直接的解决方案是将ExplicitWaitieWebDriverWaitExpectedCondition如下组合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();
    

    更好的解决方案是变得更细化,而不是ExpectedConditioninvisibilityOfElementLocated我们可以使用的ExpectedCondition那样使用elementToBeClickable,如下所示:

    WebDriverWait wait1 = new WebDriverWait(driver, 10);
    WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_element_to_be_clicked")));
    element1.click();
    
  2. 其他WebElementWebElement我们利益的永久叠加

    如果在这种情况下覆盖是永久性的,我们必须将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 :.(

当试图点击另一个元素下的一个元素时,我们通常会得到“......其他元素会收到点击”但并不总是:.(