gpt4 book ai didi

java - 如何解决 "stale element reference: element is not attached to the page document when navigating to the previous page"

转载 作者:行者123 更新时间:2023-12-02 10:42:17 24 4
gpt4 key购买 nike

我的页面上有图标,该图标可能会在该页面上多次出现。我想单击该图标,截取屏幕截图,然后返回上一页并单击其他图标并执行相同的操作:我得到

stale element reference: element is not attached to the page document

导航到上一页时”。下面是我的代码:

List<WebElement> List = driver.findElements(Loc_AttachmentIcon);

for (WebElement e : List) {
if(e.getText().contains(".JPG")) {
e.click();
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
driver.navigate().back();
}

else if(e.getText().contains(".jpg")) {
System.out.println("Contains Jpg file");
}
else if(e.getText().contains(".png")) {
System.out.println("Contains Jpg file");
}
else if(e.getText().contains(".PNG")) {
System.out.println("Contains Jpg file");
}
else if(e.getText().contains(".gif")) {
System.out.println("Contains Jpg file");
}
else if(e.getText().contains(".GIF")) {
System.out.println("Contains Jpg file");
}
else {
e.click();
}
}

最佳答案

您需要在每次迭代中重新定位元素。您可以使用索引来跟踪您的进度

int size = driver.findElements(Loc_AttachmentIcon).size();

for (int i = 0 ; i < size ; i++) {
List<WebElement> list = driver.findElements(Loc_AttachmentIcon);
WebElement e = list.get(i);

if(e.getText().contains(".JPG")) {
//...
}
}

关于java - 如何解决 "stale element reference: element is not attached to the page document when navigating to the previous page",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52854861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com