2017-02-22 74 views
0

我正在嘗試使用Selenium的Chrome Webdriver和C#自動登錄網站。運行以下代碼將引發屏幕截圖1和2中列出的錯誤。我究竟做錯了什麼?谷歌搜索錯誤似乎沒有顯示任何相關的結果。Selenium Chrome Webdriver不能正常工作

using System.Drawing.Imaging; 
using System.IO; 
using OpenQA.Selenium.Chrome; 

namespace WebDriverTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Initialize the Chrome Driver 
      using (var driver = new ChromeDriver()) 
      { 
       // Go to the home page 
       driver.Navigate().GoToUrl("https://twitter.com/");      
       // Get User Name field, Password field and Login Button 
       var userNameField = driver.FindElementById("usr"); 
      } 
     } 
    } 
} 

enter image description here

An unhandled exception of type 'OpenQA.Selenium.WebDriverException' occurred in WebDriver.dll. 
    Additional information: A exception with a null response was thrown 
sending an HTTP request to the remote WebDriver server for 
URL http://localhost:61724/session/cc7bae393b288855ed8169dade774baa/element. 
The status of the exception was ReceiveFailure, and the message was: 
The underlying connection was closed: An unexpected error occurred on a receive. 

enter image description here

+0

我相信這是一個版本不匹配。什麼是您使用的鉻和鉻驅動程序的版本? 我會建議嘗試與鉻56和最新的鉻驅動程序2.28。看起來像57鉻可能有一些問題。 –

回答

0

你剛纔GoToUrl後等待頁面加載:

driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(10)); 
+0

獲取不同的錯誤但類似的結果:在WebDriver.dll中發生未處理的類型爲「OpenQA.Selenium.WebDriverException」的異常 其他信息:意外錯誤。 System.Net.WebException:無法連接到遠程服務器---> System.Net.Sockets.SocketException:無法建立連接,因爲目標機器主動拒絕它:: 1:62419 –

0

我會等待頁面加載。

public static void WaitForElementLoad(By by, int timeoutInSeconds) 
{ 
    if (timeoutInSeconds > 0) 
    { 
     WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(timeoutInSeconds)); 
     wait.Until(ExpectedConditions.ElementIsVisible(by)); 
    } 
} 

curtosey How to wait for element to load in selenium webdriver?

0

驗證您的Chrome和ChromeDriver的版本。請參閱兼容性here。同時檢查您的Selenium版本是否過時,但這些類型的錯誤通常是因爲版本兼容性。

作爲參考,目前latests:

Chrome 56.0... 
ChromeDriver 2.27 
Selenium WebDriver 3.1.0 
0

很清楚!

頁面「http://twitter.com」中沒有標識爲「usr」的元素。

你在尋找什麼? 您正在尋找一個id屬性設置爲「usr」的頁面上的元素,並且沒有這樣的elememt。例外說它

做檢查元素plz

相關問題