我正在尝试等待加载图像从屏幕上消失
如果屏幕未加载,该元素如下所示:
<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 驱动程序错误。
造成这种情况的原因有多种:
- 元素已被完全删除。
- 元素不再附加到 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/