转载

java – 陈旧元素引用:元素不附加到页面文档

嗨,我有列表在每个部分下有多个链接.每个部分有相同的链接我需要点击每个部分下面的一个特定的链接i8已经写下面的代码,但执行它给我

“stale element reference: element is not attached to the page  document” error.

这是我的代码

提前致谢

public static void main(String[] args) throws InterruptedException 
{
    WebDriver driver = new ChromeDriver();
    driver.navigate().to("url......");
        driver.findElement(By.id("Login1_txtEmailID")).sendKeys("hourbank5@....com");
    driver.findElement(By.id("Login1_txtPassword")).sendKeys("Testing1*");
    driver.findElement(By.id("Login1_btnLogin")).click();
    List<WebElement> LeftNavLinks=driver.findElements(By.xpath("//*[@id='sliding-navigation']//a"));
    Thread.sleep(1000);
    String ben="Benefit Status";
    String[] linkTexts = new String[LeftNavLinks.size()];
    int i = 0;
    for (WebElement e : LeftNavLinks) 
    {   
        linkTexts[i] = e.getText();
        System.out.print(i+" " + linkTexts[i]+"/n");
        if(linkTexts[i].equals(ben))
        {
            String BenefitStatLi="//*[@id='sliding-navigation']/li[%s]/a";
            System.out.print(i+" " + linkTexts[i]+"/n");
                driver.findElement(By.xpath(String.format(BenefitStatLi,i))).click();
            driver.findElement(By.xpath("//* [@id='divContentHolder']/div[1]/a[1]")).click();
        }
        i++;
    }
}

}

这是HTML结构如下

<div id="ucAdminMenu_divMenu">
<ul id="sliding-navigation">
<li class="sliding-element">
<a href=" ">Claims Status</a>
</li>
<li class="sliding-element">
<a href=" ">Eligibility Status</a>
</li>
<li class="sliding-element"><h3>Section-1</h3>
</li>
<li class="sliding-element"> <a href=" ">Forms and Documents </a>
</li>
<li class="sliding-element"> <a href=" HourBank.aspx?id=002">Hour Bank </a>
</li>
<li class="sliding-element"> <h3>Section-2</h3>
</li>
<li class="sliding-element"> <a href=" " >Benefit Status</a>
</li>
<li class="sliding-element"> <a href=" ">Forms and Documents </a>
</li>
<li class="sliding-element"> <h3>Section-3</h3>
</li>
<li class="sliding-element"><a href=" ">Forms and Documents </a>
</li>
<li class="sliding-element"><h3>Testing Fund</h3>
</li>
<li class="sliding-element"><a href=" ">Benefit Status</a>
</li>
<li class="sliding-element"><a href=" ">Order ID Card</a>
</li>
</ul>
</div>

错误跟踪是

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

什么是给出异常的线?

原因是因为您已经引用的元素从DOM结构中删除

在与IEDriver一起工作时,我面临同样的问题.原因是因为JavaScript引用了我的日期引用指向一个不起眼的对象,即使它在UI上是正确的,JavaScript加载了这个元素.我使用了以下解决方法.

try{
    WebElement date = driver.findElement(By.linkText(Utility.getSheetData(path, 7, 1, 2)));
    date.click();
}
catch(org.openqa.selenium.StaleElementReferenceException ex)
{
    log.debug("Exception in finding date");
    log.debug(e);
    WebElement date = driver.findElement(By.linkText(Utility.getSheetData(path, 7, 1, 2)));
    //WebElement date = driver.findElement(By.xpath("/html/body/table/tbody/tr/td/table/tbody/tr[6]/td[4]/a/font"));
    date.click();
}

看看是否可以帮助你!

代码日志版权声明:

翻译自:http://stackoverflow.com/questions/18225997/stale-element-reference-element-is-not-attached-to-the-page-document

原文  https://codeday.me/bug/20181016/286742.html
正文到此结束
Loading...