简体   繁体   English

如何处理-在缓存中找不到元素-自从在Selenium WebDriver Java测试中查找页面以来,页面可能已更改

[英]How to handle - Element not found in the cache - perhaps the page has changed since it was looked up in Selenium WebDriver Java tests

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. 这是HTML代码,用于在表上显示删除图标,我需要单击符号以从表中删除数据,但是其ID对于每个动态的表行都在变化,因此无法在Selenium Java代码中使用ID。 I had used the className but it is also not working. 我使用过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)

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. StaleElementReferenceException指示自找到元素以来,页面源HTML已通过页面加载或JavaScript刷新,因此可能已过时。 This is true even if our object can still be found in the newly refreshed HTML. 即使仍可以在新刷新的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. 这将提取所有具有删除类且已填充ID的IMG。 You could go further; 您可以走得更远;

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? 您可以尝试这些方法还是至少提供错误的行的详细信息以及完整的堆栈跟踪信息?

Here is the solution friends, Just add an statement break; 这是解决方法的朋友,只需添加一条语句中断即可; or continue; 继续; as a last statement in for loop according to your logic. 作为您根据逻辑在for循环中的最后一条语句。 It will remove the error and the code will work fine.I am modifying the code see below... 它将消除错误并且代码可以正常工作。我正在修改代码,请参见下文...

 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 " ); } } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有Java的Selenium Webdriver:在缓存中找不到元素 - 也许页面在查找后已经发生了变化 - Selenium Webdriver with Java: Element not found in the cache - perhaps the page has changed since it was looked up 在缓存中找不到元素-自查找以来,页面可能已更改 - Element not found in cache - perhaps the page has changed since it has looked up “ StaleElementReferenceException:在缓存中找不到元素-自查找以来,页面可能已更改 - "StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up StaleElementReferenceException:在缓存中找不到元素-自查找页面以来,页面可能已更改命令持续时间或超时 - StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up Command duration or timeout 网页中无法识别的元素-在缓存中找不到元素-自查找以来,页面可能已更改 - Elements not recognised in a webpage- Element not found in the cache - perhaps the page has changed since it was looked up “在缓存中找不到元素 - 可能是页面在查找后已更改”显示 - “Element not found in the cache - perhaps the page has changed since it was looked up” displayed 在缓存中找不到元素-自查找以来页面可能已更改-为什么? - Element not found in the cache - perhaps the page has changed since it was looked up -why? StableElementReferenceException:在缓存中找不到元素 - 可能是页面自查找以来已更改 - StableElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up Selenium Webdriver,在缓存中找不到元素,可能页面自查找以来已更改 - Element not found in cache perhaps the page has changed since it has lookedup, selenium webdriver 在缓存中找不到错误元素-当我尝试从下拉列表中选择一个选项时,自查找以来页面可能已更改 - Getting error Element not found in the cache - perhaps the page has changed since it was looked up, when i am trying to select an option from dropdown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM