简体   繁体   English

Selenium Webdriver - 单击 for 循环中的多个下拉列表时出现陈旧元素异常

[英]Selenium Webdriver - Stale element exception when clicking on multiple dropdowns in a for loop

I have written down a code where I need to automate a dropdown with 4 options available.我写下了一段代码,我需要在其中自动生成一个包含 4 个可用选项的下拉菜单。 I am using a for loop to check the value in the dropdown and after that to click on every option present in the dropdown and to validate whether the application is returning the correct result based on the selection of dropdown.我正在使用 for 循环来检查下拉列表中的值,然后单击下拉列表中存在的每个选项并验证应用程序是否根据下拉列表的选择返回正确的结果。

Error found: But, after clicking on one option, it is coming out from the for loop and not executing till the size of the dropdown and returning a state element exception.发现错误:但是,在单击一个选项后,它从 for 循环中出来并且直到下拉列表的大小才执行并返回 state 元素异常。 Here I am attaching the code:这里我附上代码:

    Select select = new Select(hp.sort());
    List<WebElement> values = select.getOptions();
    
    for (int i = 0; i < values.size(); i++) {
        
            System.out.println(values.get(i).getText());
            
            if (values.get(i).getText().equals("Name (A to Z)")) {  
                
            }
            else if (values.get(i).getText().equals("Name (Z to A)")) {
                select.selectByVisibleText(values.get(i).getText());
                driver.navigate().refresh();
            }
            else if (values.get(i).getText().equals("Price (low to high)")) {
                select.selectByVisibleText(values.get(i).getText());
            }
            else if (values.get(i).getText().equals("Price (high to low)")){
                select.selectByVisibleText(values.get(i).getText());
            }
        
    }

What is StaleElementReferenceException什么是StaleElementReferenceException

In a simple words:简而言之:

Selenium has client-server architecture. Selenium 具有客户端-服务器架构。

When you store WebElement/List<WebElement> in some variable in the code, all the elements are some kind of references to the real elements on the server-side.当您将WebElement/List<WebElement>存储在代码中的某个变量中时,所有元素都是对服务器端真实元素的某种引用 And all the elements like cached .所有的元素都像缓存的。

So this references might be easily broken if the element's structure will be changed.因此,如果元素的结构将被更改,则此引用可能很容易被破坏。 The easiest way to reproduce this:重现这一点的最简单方法:

WebElement myButton = driver.findElement(...); // myButton keeps the reference to the page element
driver.navigate().refresh(); // page structure changed, all existing references became broken
myButton.click(); // this will throw StaleElementReferenceException

Solution解决方案

Track any page structure updates and refresh the element references explicitly.跟踪任何页面结构更新并显式刷新元素引用。

WebElement myButton = driver.findElement(...); // element reference created
...
driver.navigate().refresh(); // page structure changed, all existing references became broken
myButton = driver.findElement(...); // refresh the reference
myButton.click();

Also see另见

https://developer.mozilla.org/en-US/docs/Web/WebDriver/Errors/StaleElementReference https://developer.mozilla.org/en-US/docs/Web/WebDriver/Errors/StaleElementReference

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM