2017-07-12 32 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

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

相關問題