在我的測驗專案中,我有一個靜態類,其中包含許多與WebElements 進行基本互動的方法。我有兩種單獨的方法來單擊 a WebElement,一種使用該WebElement.click()方法:
public static void click(WebElement element, WebDriverWait w) {
if (element == null) {
return;
}
try {
w.until(ExpectedConditions.elementToBeClickable(element)).click();
} catch (TimeoutException ex) {
Assert.fail("Test failed, because element with locator: " element.toString().split("->")[1] " was not found on the page or unavailable");
}
}
和一個使用該Actions.click(WebElement).build().perform()方法的:
public static void click(WebElement element, Actions a, WebDriverWait w) {
if (element == null) {
return;
}
try {
a.click(w.until(ExpectedConditions.elementToBeClickable(element))).build().perform();
} catch (TimeoutException ex) {
Assert.fail("Test failed, because element with locator: " element.toString().split("->")[1] " was not found on the page or unavailable");
}
}
我還有一種從選單中查找然后單擊選項的方法:
public void selectItem(IMenuButton button) {
for (WebElement item : w.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("*[role='menuitem']")))) {
if (item.findElement(By.tagName("span")).getText().trim().equalsIgnoreCase(button.getButton())) {
// This throws StaleElementReferenceException
Interaction.click(item, a, w);
// This works
Interaction.click(item, w);
return;
}
}
Assert.fail("No menu item found for: " button.getButton());
}
當我使用Interaction.click(item, w)時,它可以作業,但是Interaction.click(item, a, w)會拋出StaleElementReferenceException,我不知道為什么。我需要在Actions.click()需要將選項滾動到視圖中時使用的方法。有任何想法嗎?
- 硒 4
- 鉻驅動程式 99
- 爪哇
uj5u.com熱心網友回復:
通常,當您向下或向上滾動時 -DOM更改。StaleElementReferenceException表示您曾經找到的元素已被移動或洗掉。當遍歷回圈內的元素時,通常是下拉或滾動視圖內的元素,您需要重新找到它們。否則你會一遍又一遍地得到這個例外。
嘗試這樣做:
try {
Interaction.click(item, a, w);
} catch (StaleElementReferenceException sere) {
// re-find the item by ID, Xpath or Css
item = driver.findElementByID("TheID");
//click again on the element
Interaction.click(item, a, w);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/439680.html
