【问题标题】:Java Selenium Error: stale element reference: element is not attached to the page documentJava Selenium 错误:过时的元素引用:元素未附加到页面文档
【发布时间】:2018-01-13 10:12:36
【问题描述】:

我使用列表元素和 For 循环条件创建了以下脚本。 请查看我的方案

场景:

  1. 打开主页 > 点击下拉菜单 > 打开下拉列表。

  2. 点击第一个选项>重定向到特定屏幕并验证标题。

  3. 回到同一个画面,同一个场景会跟随4次。

当我在 Eclipse 中执行下面的脚本时,我发现了错误。 在我下面的编码中,第一次点击有效,但第二次点击在我的下拉菜单中无效。

@Test
public void  toVerifyHelpLink() throws IOException, InterruptedException{
        File src = new File("Object_Repo.properties");
        FileInputStream fis = new FileInputStream(src);
        pro = new Properties();
        pro.load(fis);

        Thread.sleep(2000);
         WebElement linksdiv = driver.findElement(By.xpath(pro.getProperty("new_ul")));
         List<WebElement> Links = linksdiv.findElements(By.tagName("a"));
         System.out.println(Links.size());  
         for(int i=0; i<= Links.size()-1; i++)
     {
       driver.findElement(By.xpath(pro.getProperty("customerservice"))).click();
       String subCategory = Links.get(i).getText();     
       System.out.println(subCategory);
       Links.get(i).click();
            if(!subCategory.equalsIgnoreCase("404")){
                System.out.println("Title displaying as exepected");
            }
        driver.navigate().back();
        linksdiv = driver.findElement(By.xpath(pro.getProperty("new_ul")));
        Thread.sleep(2000);
    }
}

这是HTML结构如下

<div class="customer-service effect">
<a href="">customer service</a>
<ul>
<li><a href="">Help</a></li>
<li><a href="">FAQs</a></li>
<li><a href="">Contact Us</a></li>
<li><a href="">Shipping and Returns</a></li>
</ul>
</div>

【问题讨论】:

标签: selenium select arraylist


【解决方案1】:

尝试在for 循环中再添加一行:

driver.navigate().back();
linksdiv = driver.findElement(By.xpath(pro.getProperty("new_ul")));
Links = linksdiv.findElements(By.tagName("a")); // add this line to find all "a" again

【讨论】:

  • 请接受它的答案,不要让这个问题没有答案。
【解决方案2】:

过时元素异常 - 告诉您您的页面已更改,但您的 Web 元素实例未更改。这意味着当您的页面的 HTML 代码发生更改时,您应该再次找到该元素,因为 html 已更改。这就是硒的工作原理。

我还看到您的代码中存在典型错误,请查看: http://selenide.org/

并且不要使用线程休眠: https://saucelabs.com/blog/how-to-avoid-threadsleep-in-test-automation

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 2019-04-23
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 2015-02-07
    • 1970-01-01
    相关资源
    最近更新 更多