gpt4 book ai didi

java - 如何处理 - 在缓存中找不到元素 - 也许页面自从在 Selenium WebDriver Java 测试中查找以来已经发生了变化

转载 作者:行者123 更新时间:2023-12-01 06:16:47 24 4
gpt4 key购买 nike

这是在表上显示删除图标的 HTML 代码,我需要单击符号以从表中删除数据,但它的 id 会随着每个表行动态而变化,因此无法在 Selenium java 代码中使用 id。我使用了 className 但它也不起作用。

HTML

<a href="#">
<img id="489" class="delete" width="16" height="16" title="Delete Project" alt="Delete Project" src="../media/internalnotforuse/images/icons/del.png"></img>
</a>

代码

if (projectName.equals("Test")) {
System.out.println("Table Data : " + projectName);
System.out.println("Table Row " + rowCount);

rowCells.get(4).click(); // it is working fine
webdriver.findElement(By.className("delete")).click();

// webdriver.findElement(By.id("493")).click();
//it is working fine but it hard coded

for(String winHandle : sWindowHandles){
webdriver.switchTo().window(winHandle);
}

// confirm the confirmation from dialog option Yes/NO
if(deleteConfirmaton == "Yes"){
webdriver.findElement(By.xpath("//*[@id='deleteError_confirm']/table/tbody/tr[2]/td/input[1]")).click();

//webdriver.findElement(By.xpath("//*[@id='projectList']/tbody/tr[76]/td[1]")).click();
}
else{
webdriver.findElement(By.xpath("//*[@id='deleteError_confirm']/table/tbody/tr[2]/td/input[2]")).click();
System.out.println(" deleteConfirmaton is NO therefore would not be deleted " );
}
}

输出

FAILED: projectDelete
org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
Command duration or timeout: 20.07 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.37.0', revision: 'a7c61cb', time: '2013-10-18 17:15:02'
System info: host: 'TSSGMSL058', ip: '10.56.40.179', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_51'
Session ID: 9a41792f-ea05-4904-ad03-6b80396e5ccd
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=27.0.1}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

最佳答案

StaleElementReferenceException 表示自从您找到该元素以来,页面源 HTML 已通过页面加载或 JavaScript 刷新,因此可能已过时。即使我们的对象仍然可以在新刷新的 HTML 中找到,情况也是如此。

但是,您的情况看起来有点奇怪,因为它似乎没有缓存任何对象。

我想知道是否有多个元素具有相同的类,其中第一个元素很快就会过时?可能性很小,但如果没有看到带有行号的整个堆栈跟踪,就很难说。

但是,您可以尝试使用 CSS 选择器,例如;

By.Css("img.delete[id]")

这将提取所有具有删除类别的 IMG 并填充 id。你可以走得更远;

By.Css("img.delete[id][src$='del.png']")

这会进一步检查图像 src 以 del.png 结尾。

您可以尝试这些或至少提供错误行的确切详细信息和完整的堆栈跟踪吗?

关于java - 如何处理 - 在缓存中找不到元素 - 也许页面自从在 Selenium WebDriver Java 测试中查找以来已经发生了变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22778004/

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