如何解决Selenium webdriver中的ElementNotInteractableException?

在这里,我有我的代码的图像和我的错误的图像。 任何人都可以帮我解决这个问题吗?

在此处输入图像描述

在此处输入图像描述

尝试使用

Thread.sleep(10000); 

使用它时,页面中的Web元素有足够的时间加载

什么是ElementNotInteractableException

根据文档, ElementNotInteractableException是W3Cexception,抛出该exception表示虽然DOM TREE上存在一个元素,但它不处于可以与之交互的状态。

原因和解决方案:

发生ElementNotInteractableException的原因可能很多。

  1. 在我们感兴趣的WebElement WebElement上临时叠加其他WebElement

    在这种情况下,直接解决方案是将ExplicitWaitWebDriverWaitExpectedCondition结合使用,因为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(); 
  2. 通过我们感兴趣的WebElement永久覆盖其他WebElement

    如果在这种情况下覆盖是永久覆盖,我们必须将WebDriver实例WebDriverJavascriptExecutor并执行单击操作,如下所示:

     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));