c# selenium 通过导航验证所有链接 StaleElementReferenceException 控制台应用程序

标签 c# selenium webdriver console-application

当我尝试查找所有链接并在控制台应用程序中浏览它们时,我不断收到 StaleElementReferenceException 错误,我有以下代码并昨天尝试了一整天来修复它,但没有结果:

{

    static void Main(string[] args)
    {
        try
        {
            Console.WriteLine("Starting the browser...");
            IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://www.site.ro");
            System.Threading.Thread.Sleep(2000);
            Console.WriteLine("Gathering the Links...");
            List<IWebElement> links = new List<IWebElement>();
            try
            {
                foreach (IWebElement item in driver.FindElements(By.TagName("a")))
                {
                    try
                    {
                        if (item.Displayed == true)
                        {
                            item.Click();
                            Console.WriteLine("Item is displayed \a\n" + "Navigating to link...");
                        } else
                        {
                            continue;
                        }
                        Random r1 = new Random();
                        Random r2 = new Random();
                        Random r3 = new Random();
                        var last = r3.Next(1, 10) * 700;
                        var mseconds = r2.Next(1, 10) * 500;
                        var time = mseconds + r1.Next(1, 10) * 300;
                        Console.WriteLine("Waiting for " + (time + last) + " miliseconds before next link");
                        System.Threading.Thread.Sleep(time + last);
                        driver.Navigate().Back();
                        System.Threading.Thread.Sleep(2000);
                    }
                    catch (Exception e2)
                    {
                        Console.WriteLine(e2);
                        Console.ReadLine();
                    }
                    }
                }
            catch (Exception e1)
            {
                Console.WriteLine(e1);
                Console.ReadLine();
            }
            Console.WriteLine("Test finished.");
            driver.Quit();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            Console.ReadLine();
        }
    }
}

}

最佳答案

driver.FindElements(By.TagName("a")) 正在为您查找页面上的所有链接。

然后您将使用第一个链接转到另一个页面:item.Click();

最后你要回去driver.Navigate().Back();

但这不是初始页面(根据 selenium 的意见)。第一步存储的所有链接都消失了,因为您的初始页面消失了。这就是为什么您无法单击其中第二个的原因。

您需要重新查找每个driver.Navigate().Back();之后的所有链接

或者更好地将所有 href 存储到一个列表中,例如 linksList.Add(Item.getAttribute("href")); 并使用存储的 href。

关于c# selenium 通过导航验证所有链接 StaleElementReferenceException 控制台应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36638873/

相关文章:

java - 如何在断言 Selenium getCssValue ("background"返回的背景色 rgb(255,255,255) 时将#ffffff 转换为#fff 或将#fff 转换为#ffffff

java - Selenium 2.0网络驱动程序: Element is no longer attached to the DOM error using Java

c# - 在 C# 中生成函数的最佳生成函数的方法

c# - 大文件 AES 加密

python - 通过 Python 中的 Selenium 选择按钮登录

c# - 在 C# 中的 Selenium WebDriver 中使用特定的 Firefox 配置文件

Selenium webdriver - click() 成功但实际上并没有发生

c# - DependencyProperty 未在 NotifyCollectionChanged 事件上通知 UI

c# - 如何使用 Web 平台安装程序为 Web 应用程序创 build 置

firefox - Selenium 无法在 Firefox 4 上加载