java - 如何解决 ElementNotInteractableException : Element is not visible in Selenium webdriver?

标签 java selenium selenium-webdriver webdriver webdriverwait

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

enter image description here

enter image description here

最佳答案

ElementNotInteractableException

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

原因及解决方案:

ElementNotInteractableException 发生的原因有很多。

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

    在这种情况下,直接的解决方案是引入 ExplicitWait,即 WebDriverWait 与 < strong>ExpectedConditioninvisibilityOfElementLocated 如下:

    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 实例转换为 JavascriptExecutor并执行点击操作如下:

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

关于java - 如何解决 ElementNotInteractableException : Element is not visible in Selenium webdriver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43868009/

相关文章:

java - 为什么返回通用 Map 的代码在分配给通用 Map 时会生成编译器警告?

python - 遍历重新获取的元素列表

java - 使用 Matisse GUI 构建器设置最大宽度

java - 为什么这个值的输出结果总是零?

java - 如何更改cordova子项目使用的java版本

java - 使用selenium自定义Chrome webdriver保存文件下载时不提示保存或丢弃文件

java - 如何使用 WebDriver 确定列表类型(ul/li 与 span)

node.js - 从文件 Selenium node.js 添加解压的扩展

java - Salesforce中文版无法点击中文链接文本

java - 如何使用 webdriver 在鼠标悬停时保持下拉菜单打开