首页 > 解决方案 > 无法使用 Selenium Java webdriver 单击锚点 href 元素

问题描述

下面是html代码:

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

我写了以下xpath:

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

执行上述语句后,我收到以下错误:

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

我在哪里弄错了

标签: javaseleniumselenium-webdriverclickanchor

解决方案


诱导WebDriverWait() 并等待elementToBeClickable()

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

推荐阅读