陈旧的元素参考:元素未通过循环附加到页面文档 [英] stale element reference: element is not attached to the page document with loops

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

问题描述

我正在尝试浏览页面上具有多个选项卡的系统,每个选项卡上都具有多个页面链接.我知道这与从DOM中删除的元素有关,但是我不确定如何解决我的情况.

I am trying to navigate through a system that has multiple tabs on the page, each tab has multiple page links on them. I know this has to do with the elements being removed from DOM but I'm not sure exactly how to resolve this issue with my scenario.

        //Get all the elements in the top tab section
        IList<IWebElement> tabIndex = driver.FindElements(By.XPath("//*[@class='TabList ClearFix']//a[@tabindex=-1]"));
        //The first page of the frist tab is already accessed
        //Get all the page links that are in the left navigation bar, click on the page and then move onto the next one
        //Once the last page is accessed, move to the next tab and repeat.
        foreach (IWebElement element in tabIndex)
        {
            Console.WriteLine(element.Text.ToString());
            IList<IWebElement> leftIndex = driver.FindElements(By.XPath("//*[@id='LeftMenu']//a[@tabindex=-1]"));
            foreach (IWebElement lElement in leftIndex)
            {
                Console.WriteLine(lElement.Text.ToString());
            }
            element.Click();
        }

我在以下位置遇到异常:Console.WriteLine(element.Text.ToString()); 任何帮助将不胜感激.

I'm getting the exception at: Console.WriteLine(element.Text.ToString()); Any help would be greatly appreciated.

推荐答案

陈旧元素:您正在找到导致对象的元素>页面更改/重新加载>您正在尝试使用该对象.

Stale element: you are finding an element resulting in an object > page changes/reloads > you are trying to use that object.

您的主要问题是,您正在foreach循环中对先前找到的对象列表执行click动作,这些元素对象在页面更改/重新加载时丢失.

Your main issue is that you are doing a click action in a foreach loop on a list of objects previously found, those element objects are lost on page change/reload.

您需要确保在查找元素和将其用于操作之间,页面不会发生变化.

You need to make sure the page does not change between finding the element and using it in for an action.

请勿在循环中执行可能会使用循环外部对象来更改页面的操作

Do not do action in a loop that could change the page using objects from outside the loop

选项:
-计算标签并在索引中使用find
-查找所有标签,在列表中保存一些attributes并使用这些属性在循环中查找每个标签
-使用选择器基于当前标签来标识标签,例如下一个未打开的标签的选择器

Options:
- count tabs and use find in the loop by index
- find all tabs, save some attributes in a list and use those attributes to find each tab in your loop
- use a selector to identify tab based on a current tab, like a selector for the next unopened tab

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

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