首页 > 解决方案 > JavaScript xPath 命令不会在每次运行时与 Selenium Java 程序混合运行

问题描述

下面的 javascript(与 Selenium 中的 Java 混合)不会在每次运行时成功运行 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

标签: javascriptseleniumtestingxpathautomation

解决方案


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

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

推荐阅读