如何解决 ElementNotInteractableException:元素在 Selenium webdriver 中不可见?

新手上路,请多包涵

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

在此处输入图像描述

在此处输入图像描述

原文由 Aarthi 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 718
1 个回答

ElementNotInteractableException

ElementNotInteractableException 是 W3C 异常,它被抛出以指示尽管元素存在于 HTML DOM 中,但它不处于可以与之交互的状态。

原因及解决方案:

发生 ElementNotInteractableException 的原因可能有很多。

  1. 临时覆盖其他 WebElement 我们感兴趣的 WebElement

    In this case, the direct solution would have been to induce ExplicitWait ie WebDriverWait in combination with ExpectedCondition as invisibilityOfElementLocated 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`** as **`invisibilityOfElementLocated`** we can use **`ExpectedCondition`** as **`elementToBeClickable`** as follows :
    WebDriverWait wait1 = new WebDriverWait(driver, 10);
   WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_element_to_be_clicked")));
   element1.click();

  1. 其他 WebElement 永久覆盖我们感兴趣的 WebElement

如果在这种情况下覆盖是永久性的,我们必须将 WebDriver 实例转换为 JavascriptExecutor 并按如下方式执行点击操作:

    WebElement ele = driver.findElement(By.xpath("element_xpath"));
   JavascriptExecutor executor = (JavascriptExecutor)driver;
   executor.executeScript("arguments[0].click();", ele);

原文由 undetected Selenium 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题