如何处理 - 缓存中未找到元素 - 可能页面自从在 Selenium WebDriver Java 测试中查找后发生了变化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22778004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle - Element not found in the cache - perhaps the page has changed since it was looked up in Selenium WebDriver Java tests
提问by user3472488
This is HTML code to display the delete icon on table and I need to click on symbol to delete the data from table but its id is changing for every table row dynamic therefore unable to use id in Selenium java code. I had used the className
but it is also not working.
这是用于在表格上显示删除图标的 HTML 代码,我需要单击符号以从表格中删除数据,但它的 id 正在为每个表格行动态更改,因此无法在 Selenium java 代码中使用 id。我曾经使用过,className
但它也不起作用。
HTML
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>
Code
代码
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 " );
}
}
Output
输出
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)
回答by Robbie Wareham
StaleElementReferenceException
indicates that since you found the element, the page source HTML has been refreshed by page load or JavaScript, and therefore may be stale. This is true even if our object can still be found in the newly refreshed HTML.
StaleElementReferenceException
表示自从您找到该元素后,页面源 HTML 已被页面加载或 JavaScript 刷新,因此可能已过时。即使我们的对象仍然可以在新刷新的 HTML 中找到也是如此。
However, your case looks slightly strange as it doesn't appear to be caching any objects.
但是,您的案例看起来有点奇怪,因为它似乎没有缓存任何对象。
I am wondering if there are multiple elements with the same class, the first one of which quickly goes stale? Very unlikely but without seeing the entire stack trace with line numbers, it is hard to say.
我想知道是否有多个元素具有相同的类,其中第一个很快就会过时?不太可能,但没有看到带有行号的整个堆栈跟踪,很难说。
However, you could try using a CSS selector such as;
但是,您可以尝试使用 CSS 选择器,例如;
By.Css("img.delete[id]")
This will extract all IMG with a class of delete and has id populated. You could go further;
这将使用删除类提取所有 IMG 并填充 id。你可以走得更远;
By.Css("img.delete[id][src$='del.png']")
This adds a further check that the image src ends with del.png.
这会进一步检查图像 src 是否以 del.png 结尾。
Can you try these or at least provide exact details of the line which erroring and the full stack trace?
您可以尝试这些或至少提供出错行和完整堆栈跟踪的确切详细信息吗?
回答by Subodh
Here is the solution friends, Just add an statement break;or continue;as a last statement in for loop according to your logic. It will remove the error and the code will work fine.I am modifying the code see below...
下面是解决方法啦,加个语句break就行了;或继续;根据您的逻辑作为 for 循环中的最后一条语句。它将消除错误,代码将正常工作。我正在修改代码,见下文...
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);
break; //or continue;
}
// 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 " );
}
}