c# - 陈旧元素引用异常 c# Selenium Webdriver

标签 c# .net visual-studio-2012 selenium-webdriver automation

我在 Visual Studio 中使用 C# selenium Web 驱动程序来实现一些自动化目的。当我运行代码时,有时它工作得很好,但有时它会抛出错误“调用的目标已抛出异常”。当我重新启动系统,它工作正常,多次运行后再次显示错误。在用户名后提供密码时发生这种情况。网页就像用户名,提交按钮,密码,提交按钮登录网站。没有提供密码,它再次点击提交按钮。

发生异常的代码

if (ConfigurationManager.AppSettings["IsEncrypted"].ToLower() =="true")
                {

                    UserName.SendKeys(ConfigurationManager.AppSettings["UserName"]);
                    Submit.Click();
                    Thread.Sleep(2000);
                    Password.SendKeys(Base64Decode(ConfigurationManager.AppSettings["Password"]));
                    Submit.Click();
                }

这是异常(exception)

  Exception occured:Exception has been thrown by the target of an invocation.
        Exception occured:Exception has been thrown by the target of an invocation.
        Exception occured System.Reflection.TargetInvocationException: Exception has bee
        n thrown by the target of an invocation. ---> OpenQA.Selenium.StaleElementRefere
        nceException: stale element reference: element is not attached to the page docum
        ent
          (Session info: chrome=58.0.3029.110)
          (Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9e
        ed),platform=Windows NT 6.1.7601 SP1 x86_64)
           at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response erro
        rResponse)
           at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecu
        te, Dictionary`2 parameters)
           at OpenQA.Selenium.Remote.RemoteWebElement.Click()
           --- End of inner exception stack trace ---
           at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments,
         Signature sig, Boolean constructor)
           at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Objec
        t[] parameters, Object[] arguments)
           at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invoke
        Attr, Binder binder, Object[] parameters, CultureInfo culture)
           at OpenQA.Selenium.Support.PageObjects.WebDriverObjectProxy.InvokeMethod(IMet
        hodCallMessage msg, Object representedValue)
           at OpenQA.Selenium.Support.PageObjects.WebElementProxy.Invoke(IMessage msg)
           at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgDa
        ta, Int32 type)
           at OpenQA.Selenium.IWebElement.Click()
           at JiraExtract.PageObjects.LoginPage.LoginToApplication() in C:/Program.cs:line 72
           at JiraExtract.Automation.AutomationTest() in c:/Program.cs:line 80
           at JiraExtractor.Program.Main(String[] args) in c:/Program.cs:line 14

最佳答案

我假设您正在使用 PageObject 工厂来初始化您的 Web 元素。

您可以注意到,您的第一个 Submit.Click(); 执行时没有任何错误,但是当您输入用户名并尝试再次单击“提交”按钮时,它会抛出一个 Stale Element异常

在以下情况下会生成陈旧元素异常:

  • 该元素已被完全删除

  • 元素不再附加到 DOM

就您的情况而言,我认为您的页面将Submit按钮与DOM分离,并在您输入用户名时重新附加。

解决方案:尝试调用PageFactory.initElements(getDriver(), ClassWhichHasSubmitButton.class);

编辑: 您应该在输入密码之后、单击提交按钮之前调用此函数PageFactory.initElements(getDriver(), ClassWhichHasSubmitButton.class);

关于c# - 陈旧元素引用异常 c# Selenium Webdriver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45055241/

相关文章:

c# - 如何使用图标呈现数据绑定(bind)的 WinForms DataGridView 列?

c# - C# 和 Godaddy 服务器发送电子邮件错误

c# - 为什么我不能像隐藏属性那样隐藏继承的事件?

c# - .net 4.5 是否支持 NFC Proximity API

c# - 如果 C# 中的关键字未在系统类中定义,那么它们在 CLR 中的哪里?

c# - System.IO.FileNotFoundException 出现在 mscorlib.dll 中?

visual-studio - Visual Studio发布不将所有DLL复制到发布文件夹

c# - 对于 nunit 测试夹具,多个类别是否会导致多个测试?

c# - 更改 Visual Studio 2012 C# 模数格式

c# - 使用Windows.Media.Transcoding从WAV创建m4a