简体   繁体   English

(StaleElementException:Selenium)我该如何处理?

[英](StaleElementException:Selenium) How do I handle this?

This is my first time first day working on selenium and I have no hands on experience on Web Technologies in depth either. 这是我第一天第一次从事硒研究,也没有深入的Web技术经验。

Working around, I have been facing StaleElementException while i try to access a particular object on the DOM. 解决方法,在尝试访问DOM上的特定对象时,我一直面临StaleElementException。

Following Method handles all the task: 以下方法处理所有任务:

private void extract(WebDriver driver) {
    try {

        List<WebElement> rows = driver.findElements(By.xpath("//*[@id='gvSearchResults']/tbody/tr"));

        for (WebElement row : rows) {
            WebElement columns = row.findElement(By.xpath("./td[1]/a"));

            if (assertAndVerifyElement(By.xpath("//*[@id='gvSearchResults']/tbody/tr/td[1]/a"))) {
                columns.click();
            }

            List<WebElement> elements = driver
                    .findElements(By.xpath("//*[@id='ctl00_MainContent_pnlDetailsInd']/table/tbody/tr"));

            for (WebElement element : elements) {
                WebElement values = element.findElement(By.xpath("./td[1]"));
                System.out.print(values.getText() + " ");
                WebElement values2 = element.findElement(By.xpath("./td[2]"));
                System.out.println(values2.getText());
            }
            if(assertAndVerifyElement(By.xpath("//*[@id='ctl00_MainContent_btnBack']")))
                driver.findElement(By.xpath("//*[@id='ctl00_MainContent_btnBack']")).click();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } 
}

The Assertion logic goes here: 断言逻辑在这里:

public boolean assertAndVerifyElement(By element) throws InterruptedException {
    boolean isPresent = false;

    for (int i = 0; i < 5; i++) {
        try {
            if (driver.findElement(element) != null) {
                isPresent = true;
                break;
            }
        } catch (Exception e) {
            // System.out.println(e.getLocalizedMessage());
            Thread.sleep(1000);
        }
    }
    Assert.assertTrue("\"" + element + "\" is not present.", isPresent);
    return isPresent;
}

I have tried few solutions asking me to use wait until expected conditions, but none of them worked. 我尝试过很少的解决方案,要求我使用等到预期的条件,但没有一个起作用。

Also, It would be appreciated if you point out any bad design practices I might be using in the above sample. 此外,如果您指出我在以上示例中可能使用的任何不良设计做法,也将不胜感激。

I can give you the idea to overcome staleness. 我可以给你克服陈旧的想法。

Generally we will be getting the Stale Exception if the element attributes or something is changed after initiating the webelement. 通常,如果在启动Webelement后更改了元素属性或某些内容,则将获得Stale Exception。 For example, in some cases if user tries to click on the same element on the same page but after page refresh, gets staleelement exception. 例如,在某些情况下,如果用户尝试在同一页面上单击同一元素,但在页面刷新后单击,则会获得跟踪例外。

To overcome this, we can create the fresh webelement in case if the page is changed or refreshed. 为了克服这个问题,我们可以创建新的Web元素,以防页面被更改或刷新。 Below code can give you some idea. 下面的代码可以给您一些想法。

Example: 例:

webElement element = driver.findElement(by.xpath("//*[@id='StackOverflow']"));
element.click();
//page is refreshed
element.click();//This will obviously throw stale exception

To overcome this, we can store the xpath in some string and use it create a fresh webelement as we go. 为了克服这个问题,我们可以将xpath存储在某个字符串中,并在使用时使用它创建一个新的webelement。

String xpath = "//*[@id='StackOverflow']";
driver.findElement(by.xpath(xpath)).click();
//page has been refreshed. Now create a new element and work on it
driver.findElement(by.xpath(xpath)).click();   //This works

Another example: 另一个例子:

for(int i = 0; i<5; i++)
{
  String value = driver.findElement(by.xpath("//.....["+i+"]")).getText);
  System.out.println(value);
}

Hope this helps you. 希望这对您有所帮助。 Thanks 谢谢

The StaleElementException occurs when the webelement in question is changed on the dom and the initial reference to that webelement is lost. 当所讨论的Web元素在dom上更改并且丢失对该Webelement的初始引用时,将发生StaleElementException。

You can search for the webelement again 您可以再次搜索网络元素

try this 尝试这个

try:
 element = self.find_element_by_class('')
 element.click()
except StaleElementReferenceException:
 element = self.find_element_by_class('')
 element.click()

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

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