【问题标题】:Could not click on anchor href element using Selenium Java webdriver无法使用 Selenium Java webdriver 单击锚点 href 元素
【发布时间】:2020-11-10 00:03:36
【问题描述】:

下面是html代码:

<a href="javascript:void(0)">
STK10000251
VESUVIUS29
Vesuvius India Ltd  
</a>

我已经写了以下xpath:

driver.findElement(By.xpath("//a[contains(text(), 'STK10000251')]")).click();   

执行上述语句后,出现以下错误:

线程“main”中的异常 org.openqa.selenium.StaleElementReferenceException:过时的元素 参考:元素未附加到页面文档

我哪里弄错了

【问题讨论】:

  • StaleElementReferenceException 出现在元素未附加到页面时。您是否使用显式等待点击?
  • @KunduK 不,我没有...
  • 哇!成功了!!!

标签: java selenium selenium-webdriver click anchor


【解决方案1】:

诱导WebDriverWait()并等待elementToBeClickable()

WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(text(), 'STK10000251')]")));
element.click();

【讨论】:

  • 能否请您解释一下,StaleElementReferenceException 是如何与等待相关联的。
  • @Sree:由于当 webdriver 尝试与未附加到页面的元素交互时应用程序运行缓慢,有时它会变得陈旧。但是我希望 no such element 错误而不是 StaleElementReferenceException
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-13
  • 2015-09-01
  • 2023-03-10
  • 2018-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多