过时的元素引用:元素未附加到页面文档 [英] stale element reference: element is not attached to the page document

查看:29
本文介绍了过时的元素引用:元素未附加到页面文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,每个部分下都有多个链接.每个部分都有相同的链接,我需要单击每个部分下的特定链接.我已经编写了以下代码,但是当它执行时,它给了我 stale element reference: element is not attach to the page document 错误.

这是我的代码:

public static void main(String[] args) 抛出 InterruptedException{WebDriver 驱动程序 = 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();列表LeftNavLinks=driver.findElements(By.xpath("///*[@id='sliding-navigation']//a"));线程睡眠(1000);String ben="福利状态";String[] linkTexts = new String[LeftNavLinks.size()];int i = 0;for (WebElement e : LeftNavLinks){linkTexts[i] = e.getText();System.out.print(i+" " + linkTexts[i]+"
");if(linkTexts[i].equals(ben)){String BenefitStatLi="//*[@id='sliding-navigation']/li[%s]/a";System.out.print(i+" " + linkTexts[i]+"
");driver.findElement(By.xpath(String.format(BenefitStatLi,i))).click();driver.findElement(By.xpath("//* [@id='divContentHolder']/div[1]/a[1]")).click();}我++;}}}

HTML 结构如下

<ul id="滑动导航"><li class="sliding-element"><a href=" ">索赔状态</a><li class="sliding-element"><a href=" ">资格状态</a><li class="sliding-element"><h3>Section-1</h3><li class="sliding-element"><a href=" ">表格和文件</a><li class="sliding-element"><a href="HourBank.aspx?id=002">Hour Bank</a><li class="sliding-element"><h3>Section-2</h3><li class="sliding-element"><a href=" ">福利状况</a><li class="sliding-element"><a href=" ">表格和文件</a><li class="sliding-element"><h3>Section-3</h3><li class="sliding-element"><a href=" ">表格和文件</a><li class="sliding-element"><h3>测试基金</h3><li class="sliding-element"><a href=" ">福利状况</a><li class="sliding-element"><a href=" ">订购身份证</a>

错误跟踪是:

 线程main"中的异常org.openqa.selenium.StaleElementReferenceException:过时的元素参考:元素未附加到页面文档

解决方案

出现异常的那一行是什么??

这是因为你所引用的元素从DOM结构中移除了

我在使用 IEDriver 时遇到了同样的问题.原因是因为 javascript 在我引用后又加载了一次元素,所以我的日期引用指向一个不存在的对象,即使它就在 UI 中.我使用了以下解决方法:

尝试{WebElement 日期 = driver.findElement(By.linkText(Utility.getSheetData(path, 7, 1, 2)));日期.点击();}捕获(org.openqa.selenium.StaleElementReferenceException ex){WebElement 日期 = driver.findElement(By.linkText(Utility.getSheetData(path, 7, 1, 2)));日期.点击();}

看看能不能帮到你!

I have list which has multiple links under each section. Each section has same links I need to click a particular link under each section. I have written the below code but when it executes it gives me stale element reference: element is not attached to the page document error.

This is my code:

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]+"
");
        if(linkTexts[i].equals(ben))
        {
            String BenefitStatLi="//*[@id='sliding-navigation']/li[%s]/a";
            System.out.print(i+" " + linkTexts[i]+"
");
                driver.findElement(By.xpath(String.format(BenefitStatLi,i))).click();
            driver.findElement(By.xpath("//* [@id='divContentHolder']/div[1]/a[1]")).click();
        }
        i++;
    }
}

}

This is the HTML structure is as below

<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>

The Error Trace is:

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

解决方案

What is the line which gives exception ??

The reason for this is because the element to which you have referred is removed from the DOM structure

I was facing the same problem while working with IEDriver. The reason was because javascript loaded the element one more time after i have referred so my date reference pointed to a nonexistent object even if it was right there in the UI. I used the following workaround:

try {
    WebElement date = driver.findElement(By.linkText(Utility.getSheetData(path, 7, 1, 2)));
    date.click();
}
catch(org.openqa.selenium.StaleElementReferenceException ex)
{
    WebElement date = driver.findElement(By.linkText(Utility.getSheetData(path, 7, 1, 2)));
    date.click();
}

See if the same can help you !

这篇关于过时的元素引用:元素未附加到页面文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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