英文:
is there a way to handle stale Element in selenium?
问题
我正在导航到一个页面,页面上有一个菜单列表,我想检查是否可以点击所有菜单并获得预期的结果,因为有些菜单选项将我导航到不同的屏幕;在第一次导航后,我回到原始屏幕来检查剩下的选项,但在第一次导航后,我的元素变得无效。
for (int i = 0; i < menu.size(); i++) {
String menuName = menu.get(i).getText();
if (menuName.contains("Home")) {
menu.get(i).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
String pageDetails = driver.getCurrentUrl();
driver.navigate().back();
driver.navigate().refresh();
}
}
英文:
i was navigating to a page which had a list of menu in that page and i wanted to check if i can click on all the menu and get expected outcome, since some of the menu option navigated me to different screen ; i navigated back to original screen to check remaining options but after first navigation my elements are becoming stale
for (int i = 0; i < menu.size(); i++) {
String menuName = menu.get(i).getText();
if (menuName.contains("Home")) {
menu.get(i).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
String pageDetails = driver.getCurrentUrl();
driver.navigate().back();
driver.navigate().refresh();
}
}
答案1
得分: 0
代码部分不需要翻译,以下是翻译好的内容:
"Well, the behaviour is completely correct from testing perspective. (Really, who knows what's happened to the original page while you were 'outside'? Elements need to be refreshed.)
Some frameworks (like Selenide) have methods that use dynamic refresh of elements during iteration (see asDynamicIterable) but it is Not Recommended.
Try to open your menu items in other tab and verify it after you have opened them all. Or work with other windows tabs not leaving current one. See this article
英文:
Well, the behaviour is completely correct from testing perspective. (Really, who knows what's happened to the original page while you were "outside"? Elements need to be refreshed.)
Some frameworks (like Selenide) have methods that use dynamic refresh of elements during iteration (see asDynamicIterable) but it is Not Recommended.
Try to open your menu items in other tab and verify it after you have opened them all. Or work with other windows tabs not leaving current one. See this article
答案2
得分: 0
可能会发生异常,当页面加载时,元素引用变得陈旧或在DOM中被重新排列。但您可以通过以下代码来处理它。
WebDriverWait w = new WebDriverWait(driver, seconds);
w.until(ExpectedConditions.invisibilityOfElementLocated(locator/Xpath));
英文:
Probably statle Exception occurs when the page gets loaded and the element reference get staled or shuffled in DOM.but you can handle it through the below code.
WebDriverWait w = new WebDriverWait(driver, seconds);
w.until(ExpectedConditions.invisibilityOfElementLocated(locator/Xpath));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论