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

时间:2014-04-01 06:03:58

标签: java selenium selenium-webdriver webdriver

这是用于在表格上显示删除图标的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)

2 个答案:

答案 0 :(得分:2)

StaleElementReferenceException表示自从您找到该元素后,页面源HTML已经被页面加载或JavaScript刷新,因此可能是陈旧的。即使我们的对象仍然可以在新刷新的HTML中找到,也是如此。

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

我想知道是否有多个具有相同类的元素,第一个元素很快就会过时了?非常不可能,但没有看到整个堆栈跟踪的行号,很难说。

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

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

这将使用一类删除提取所有IMG并填充id。你可以走得更远;

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

这进一步增加了图像src以del.png结尾的检查。

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

答案 1 :(得分:-2)

以下是解决方案的朋友,只需根据您的逻辑在for循环中添加声明 break; continue; 作为最后一个声明。它将删除错误,代码将正常工作。我正在修改代码,见下文......

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