2017-07-12 30 views
1

我在Visual Studio中使用C#selenium web驱动程序来实现一些自动化目的。当我运行我的代码时,它有时会完美运行,但有时它会抛出一个错误“Exception has been displayed by the目标的调用“。当我重新启动我的系统时,它工作正常,再次显示错误多次运行后。它发生在给用户名后的密码。网页就像用户名,提交按钮,密码,提交按钮登录到网站。没有给密码,它再次点击提交按钮。陈旧的元素引用异常c#Selenium Webdriver

代码中的异常发生的历史

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

我假设错误在'Submit.Click();'。为什么你需要点击两次?错误是否在第二次提交时发生? – Buaban

+0

网站设计就像那样。首先我需要给用户名提交它,然后我应该给密码并再次提交,login.Error,因为我可以看到“给用户名后没有给密码它是再次点击提交”。 –

+0

在您的代码中的'密码'是静态类还是WebElement? – Buaban

回答

0

什么@Rameshwar说是真的,你应该调用此。提交按钮从DOM分离。

解决方案: 不要直接单击提交按钮,而是直到找到元素,然后单击。

{ 
    Password.SendKeys(Base64Decode(ConfigurationManager.AppSettings["Password"])); 
    Click(By.Id("login-submit")); 
    } 


public bool Click(By by) 
     { 
      bool status = false; 
      int i = 0; 
      while (i==0) 
      try 
      { 
      driver.FindElement(by).Click(); 
      status = true; 
      break; 
      } 
      catch (StaleElementReferenceException e) 
      { 
      } 
      return status; 
     } 
0

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

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

  • 的元素已经被全部删除

  • 元素不再附加到DOM

在你的情况,我:当产生

陈旧元素异常假设你的页面将Submit按钮从DOM中分离出来,并在你输入用户名时重新连接。

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

编辑: 您输入密码,点击之前提交按钮PageFactory.initElements(getDriver(), ClassWhichHasSubmitButton.class);

+0

是的,我做到了,但没有奏效。 –

相关问题