JavaScript xPath命令不会在每次运行时与Selenium Java程序混合运行

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript xPath命令不会在每次运行时与Selenium Java程序混合运行相关的知识,希望对你有一定的参考价值。

以下javascript(与Java在Selenium中混合在一起)未成功运行xpath命令每次运行。但是我的Java命令成功运行,这是我遇到问题的xpath。 (换句话说,有时xpath命令成功运行,而在其他时候则没有)。我将jdk从13更改为jdk8,但这没有用。我不知道需要什么。

我是学习自动化测试的新手,我正在自学。

这是命令行:

driver.findElement(By.xpath("//button[@type='button' and @data-test-id='checkbox']")).click();

错误响应:

JavaScript error: , line 0: NotAllowedError: The play method is not
allowed by the user agent or the platform in the current context,
possibly because the user denied permission.
Exception in thread "main"
org.openqa.selenium.StaleElementReferenceException: The element
reference of <button class="c27KHO0_n b_0 M_0 i_0 I_T y_Z2uhb3X
A_6EqO r_P C_q cvhIH6_T ir3_1JO2M7 P_0" type="button"> is stale;
either the element is no longer attached to the DOM, it is not
in the current frame context, or the document has been refreshed
答案

StaleElementReferenceException:当元素未附加到DOM时发生,请使用显式等待和检查:

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement button = wait.until(
        ExpectedConditions.elementToBeClickable(By.xpath("//button[@type='button' and @data-test-id='checkbox']"))).click();

以上是关于JavaScript xPath命令不会在每次运行时与Selenium Java程序混合运行的主要内容,如果未能解决你的问题,请参考以下文章

我想在 javascript 或 Jquery 中使用 XPath 3.0 语言支持

每次加载页面时运行 javascript

selenium使用Xpath+CSS+JavaScript+jQuery的定位方法(治疗selenium各种定位不到,点击不了的并发症)

selenium使用Xpath+CSS+JavaScript+jQuery的定位方法(治疗selenium各种定位不到,点击不了的并发症)

webpack 构建后运行命令

在有效的 PHP query() XPath 中转换 Javascript XPath |规范化 JS XPath --> PHP