【问题标题】:How to Handing sub-links --"stale element reference: element is not attached to the page document"如何处理子链接——“过时的元素引用:元素未附加到页面文档”
【发布时间】:2016-09-19 12:04:34
【问题描述】:

目标:脚本逐个点击子链接并验证页面是否存在

场景: 当我将鼠标悬停在类别链接上时 - 它会在右侧显示子类别链接(列表):

当我收回悬停时 - 链接(列表)不会显示:

自动化: 我试图将鼠标悬停在类别链接上!! 从而捕获了Category链接的Xpath!! 鼠标悬停的应用动作!! 鼠标悬停在 0 索引(开始)处捕获子链接的 x 路径! 应用“For Each loop”(高级For循环)来一一捕获子链接x路径!! 在上面的for循环中,使用“action”点击子链接!!

问题:它点击索引 0 处的子链接,在以后的迭代中,即使鼠标悬停在类别链接上,它也无法捕捉到子链接...

如何在第二,第三......迭代......

以下是代码:

public static WebElement Add_EditCorpUser(WebDriver driver) throws InterruptedException
{
    driver.switchTo().defaultContent();
    driver.switchTo().frame(1);
    Actions action = new Actions(driver);   


    element=driver.findElement(By.xpath(".//td[@title='Security Admin']/following::td[1]/span[contains(text(),'Security')]"));
    action.moveToElement(element).build().perform();    
    List<WebElement> arrayList=driver.findElements(By.xpath(".//table[@class='hideHeader']//span[@id='subMenu']//div[@id='i_2sub']//tbody/tr"));

    List<String> linksname = new ArrayList<>();
     for (WebElement w:arrayList) {
            linksname.add(w.getText());
            System.out.println("value ---------------- --"+ linksname);
            action.moveToElement(w).click().build().perform();
            Thread.sleep(10000);

            driver.switchTo().defaultContent();

            driver.switchTo().frame(1);

            action.moveToElement(driver.findElement(By.xpath(".//td[@title='Security Admin']/following::td[1]/span[contains(text(),'Security')]"))).build().perform();  



            Thread.sleep(10000);

     }

----------------------------------- -------------------------------------------------- -------------------------------------------------- --------------------------------根据注释修改代码------------- ---------

 public static WebElement element=null;
     public static WebElement Add_EditCorpUser(WebDriver driver) throws InterruptedException
{
    driver.switchTo().defaultContent();
    driver.switchTo().frame(1);
    Actions action = new Actions(driver);   


    element=driver.findElement(By.xpath(".//td[@title='Security Admin']/following::td[1]/span[contains(text(),'Security')]"));
    action.moveToElement(element).build().perform();    
    List<WebElement> arrayList=driver.findElements(By.xpath(".//table[@class='hideHeader']//span[@id='subMenu']//div[@id='i_2sub']//tbody/tr"));

    String[] xpa=new String[arrayList.size()];
    for(int i=1;i<arrayList.size();i++)
    {
        xpa[i]="\".//table[@class='hideHeader']//span[@id='subMenu']//div[@id='i_2sub']//tbody/tr["+i+"]\"";


    }

    for(String a: xpa )
    {
        if(a!=null){
            System.out.println(a);
            action.moveToElement(driver.findElement(By.xpath(a))).click().build().perform();
            Thread.sleep(10000);
            driver.switchTo().defaultContent();
            driver.switchTo().frame(1);
            System.out.println("Switched to new frame");
            action.moveToElement(element).build().perform();    

        }

【问题讨论】:

    标签: selenium selenium-webdriver automation testng selenium-grid


    【解决方案1】:

    当您搜索一个元素并且在对其执行任何操作之前页面已更改/重新加载时,您会得到陈旧的元素。

    在页面中执行任何操作之前,请确保页面已完全加载。 先等待页面加载完毕 -> 然后找到元素并执行动作。

    如果您将所有链接保存在一个数组中,并且您想单击其中的每一个链接,它将无法正常工作。
    您需要有一个包含链接选择器的数组,并在您的 for 循环中使用该数组。

    在你有无效选择器的第二个代码上,你有额外的双引号。试试

    
    xpa[i]="//table[@class='hideHeader']//span[@id='subMenu']//div[@id='i_2sub']//tbody/tr["+i+"]";
    
    

    记住 xpath 是一个字符串,使用连接,打印字符串并在浏览器中检查并更新以获得有效的 xpath。

    【讨论】:

    • 我写了一个新代码,在那里我收集了子链接的 x 路径并试图点击它们:但我面临的是---我已经收集了变量“a”或“中的 xpaths xpa" - 当我尝试使用代码中所示的 a 或 xpa 将鼠标悬停时,它会引发错误.....但是当我将 xpath 放入它运行代码的操作时,因此我无法进行使用存储的 x 路径.......当我打印 x 路径时,一切都很好,但是当我尝试通过变量使用它时......抛出新添加的图像文件中所示的错误
    猜你喜欢
    • 2021-12-15
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多