【问题标题】:JavaScript xPath command does not run co-mingled with Selenium Java program on every runJavaScript xPath 命令不会在每次运行时与 Selenium Java 程序混合运行
【发布时间】:2020-07-08 07:44:49
【问题描述】:

下面的 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

【问题讨论】:

  • oes not run xpath command successfully on every run 你得到什么错误信息?

标签: javascript selenium testing xpath automation


【解决方案1】:

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

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

【讨论】:

  • 显式等待和 stateElement Ref 不相关。 Explicit wait 将用于确保脚本等到满足显式条件(意思是直到在上面的代码中可以点击元素)。其中staleElement Reference Exception 在您引用脚本中元素的旧元素引用时出现。
  • StaleElementReferenceException 是由于 findelement/findelements 方法访问的元素不可用。我添加了 WebDriverWait 以确保元素在对元素执行任何操作之前可用(如果您对该元素的可用性有疑问)
  • 如果该元素不存在于 DOM 中,您将收到 No Such Element 异常。是的,您的想法和解决方案很好,而 stale element exception 一词在这里具有误导性。
  • 当我们试图与之交互的元素被销毁然后重新创建时,会发生陈旧的元素引用异常。由于 DOM 中元素的这个引用变得陈旧并且我们没有获得元素的引用
  • @supputuri 感谢您的等待条件。当 click() 添加到命令末尾时,我收到:类型不匹配:无法从 void 转换为 WebElement。所以,我删除了 click(),所以它的内容如下: WebDriverWait wait = new WebDriverWait(driver, 30); WebElement button=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@type='button' and @data-test-id='checkbox']"))); driver.findElement(By.xpath("//button[@type='button' and @data-test-id='checkbox']")).click();
猜你喜欢
  • 1970-01-01
  • 2012-01-18
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 2015-03-16
  • 2012-04-10
  • 2019-05-16
相关资源
最近更新 更多