![](/img/trans.png)
[英]Element is no longer attached to the DOM error in selenium webdriver
[英]Error in Selenium Webdriver finding element in DOM
我試圖在Selenium Webdriver上運行測試。
有時,我會收到一條錯誤消息,指出該元素已過時且無法找到,當我單擊網站第二頁上的按鈕時,會發生這種情況:
bookNowButton.click();
它是間歇性的,不會在每次測試運行中都發生
我介紹了一個“等待”,但似乎沒有什么不同
以前有沒有人發生過這種情況,您如何解決?
錯誤信息:
test.java > Test - Chrome > com.bookinggo.ticketed.uiendtoend.TicketedReturnJourneyTest > return_tc03 FAILED
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=76.0.3809.25)
(Driver info: chromedriver=76.0.3809.25 (a0c95f440512e06df1c9c206f2d79cc20be18bb1-refs/branch-heads/3809@{#271}),platform=Mac OS X 10.14.5 x86_64) (WARNING: The server did not provide any stacktrace information)
完整代碼:
public boolean waitForPageCheckOutPageToLoad() {
try {
WebDriverWait waitPage = new WebDriverWait(driver, 30);
waitPage.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='bui-button__text']")));
return TRUE;
} catch (NoSuchElementException e) {
return FALSE;
}
}
public void clickBookNowButton()
{
waitForPageCheckOutPageToLoad();
WebElement bookNowButton = driver.findElement(By.xpath("//span[@class='bui-button__text']"));
bookNowButton.click();
}
在以下兩種情況之一中會引發陳舊的元素引用異常,第一種情況比第二種情況更常見:
元素不再附加到DOM。
嘗試使用此代碼等待元素,直到可以訪問為止。
new WebDriverWait(driver, timeout)
.ignoring(StaleElementReferenceException.class)
.until(new Predicate<WebDriver>() {
@Override
public boolean apply(@Nullable WebDriver driver) {
driver.findElement(By.id("checkoutLink")).click();
return true;
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.