java - 未知错误: unhandled inspector error: {"code":-32000 ,"message" :"Cannot find context with specified id"}

标签 java selenium selenium-webdriver driver webdriverwait

我正在尝试等待加载图像从屏幕上消失

如果屏幕未加载,该元素如下所示:

<div id="divWait" style="cursor: wait; position: absolute; left: 0%; top: 0%; background: transparent; padding: 3px; width: 100%; height: 100%; display: none;" xpath="1">

当屏幕加载时,元素看起来像:

   <div id="divWait" style="cursor: wait; position: absolute; left: 0%; top: 0%; background: transparent; padding: 3px; width: 100%; height: 100%; " xpath="1">

显示消失

我为此编写的代码行:

w.until(ExpectedConditions.attributeContains(driver.findElement(By.xpath("//*[@id=\"divWait\"]")), "display", "none"));

值得一提的是,我的元素位于 iframe 中,但我已经处理并确保我可以解析 Iframe 中的其他元素,我还确保上面使用的 xpath 是有效的(复制为来自 html 的 xpath)

其他时候我会遇到以下异常

stale element reference: element is not attached to the page document

最佳答案

过时元素引用错误是由于引用的 Web 元素不再附加到 DOM 而发生的 Web 驱动程序错误。

造成这种情况的原因有多种:

  1. 元素已被完全删除。
  2. 元素不再附加到 DOM。

当文档节点从 DOM 中删除且其 Web 元素引用将失效时,就会发生过时

尝试以下代码来解决您的错误:

WebDriverWait wait = new WebDriverWait(driver,30);              

wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("divWait")));

注意:请将以下导入添加到您的解决方案中

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

关于java - 未知错误: unhandled inspector error: {"code":-32000 ,"message" :"Cannot find context with specified id"},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61294170/

相关文章:

java - 使用 Selenium 和 Cucumber JVM-Java 测试一组链接

java - 在可观察链中应用调度器两次(使用 compose)

java - 适用于 Java 应用程序的 Heroku New Relic Add-on 无法打开 newrelic.jar

java - ORMLite 的当前时间戳?

java - 使用 Selenium 单例驱动程序类是常见做法吗?

java - 运行跨浏览器测试时遇到问题

java - Spring & dirtyscontext & AnnotationTransactionAspect

javascript - 期望(array_values).toBeGreaterThan(Some_Value)

selenium - 构建你自己的 SeleniumHQ/docker-selenium docker 镜像

java - 将返回的变量传递给其他方法