(StaleElementException:Selenium)我该如何处理? [英] (StaleElementException:Selenium) How do I handle this?

查看:109
本文介绍了(StaleElementException:Selenium)我该如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次参与硒工作的第一天,我也没有深入了解Web技术的经验。

This is my first time first day working on selenium and I have no hands on experience on Web Technologies in depth either.

解决方法,我一直面临着StaleElementException当我尝试访问DOM上的特定对象时。

Working around, I have been facing StaleElementException while i try to access a particular object on the DOM.

以下方法处理所有任务:

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

断言逻辑在这里:

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.

推荐答案

当有问题的webelement在dom上更改并且对该webelement的初始引用丢失时,就会发生StaleElementException。

The StaleElementException occurs when the webelement in question is changed on the dom and the initial reference to that webelement is lost.

您可以再次搜索webelement

You can search for the webelement again

试试这个

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

这篇关于(StaleElementException:Selenium)我该如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆