2013-12-15 276 views
0

SetUpTest:硒找不到元素

public void SetupTest() 
{ 
    IWebDriver driver = new ChromeDriver(); 

    selenium = new DefaultSelenium(
     "localhost", 
     4444, 
     "*googlechrome C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", 
     "http://localhost"); 
    selenium.Start(); 
    verificationErrors = new StringBuilder(); 
} 

測試功能:

[Test] 
public void LoginTest() 
{ 
    selenium.Open("http://localhost:8085/"); 

    // login 
    for (int second = 0; ; second++) 
    { 
     if (second >= 60) Assert.Fail("timeout"); 
     try 
     { 
      if (IsElementPresent(By.CssSelector("#username"))) break; 
     } 
     catch (Exception) 
     { } 
     Thread.Sleep(1000); 
    } 

    driver.FindElement(By.Id("username")).SendKeys("admin"); 
    driver.FindElement(By.Id("password")).SendKeys("123456"); 
    driver.FindElement(By.CssSelector(".btn.btn-primary")).Click(); 
} 

private bool IsElementPresent(By by) 
{ 
    try 
    { 
     driver.FindElement(by); 
     return true; 
    } 
    catch (NoSuchElementException) 
    { 
     return false; 
    } 
} 

我在這個鏈接下載Chrome驅動程序:http://chromedriver.storage.googleapis.com/index.html

最新的版本是2.7。

我的鍍鉻版本是31.0.1650.63。

問題是,驅動程序找不到元素,儘管它存在於視圖中。

如何使它工作?

+0

您似乎在這裏混合技術。你爲什麼一起使用Selenium RC和WebDriver?我並不感到驚訝,這是行不通的。重新開始。閱讀文檔。 – Arran

回答

2

我會推薦Selenium IDE並在Selenium IDE中錄製時手動登錄。它將幫助您識別要選擇的元素的正確名稱。

我用類似的東西

using Selenium; 

ISelenium sel = new DefaultSelenium("localhost", 4444, "*firefox", ""); 
sel.Start(); 
sel.Open("www.whateveryourwebsideis.com"); 

sel.Type("id=user_email", "username"); 
sel.Type("id=user_password", "password"); 
sel.Click("name=commit"); 

更新: 在我看來,如果你不使用你的IDriver進行導航。

你有

selenium.Open("http://localhost:8085/"); 

,但我猜你應該使用

driver.Navigate().GoToUrl("http://localhost:8085/"); 

嘗試

string htmlSource = driver.PageSource; 

加載頁面後,檢查是否真正遇到任何HTML搜索元素。

我只是嘗試安裝ChromeDriver,但它並沒有真正的工作,我實際上並不需要它,所以我害怕我不得不讓它找到解決方案...祝你好運。

+0

這是工作!請幫助我使用'driver'工作:)爲什麼驅動程序無法識別正確的元素? – Stiger

+0

由於某些原因,驅動程序從來沒有爲我工作,所以我只是使用ISelenium類。不知道有什麼區別,爲什麼都存在......但是因爲ISelenium對我來說工作得很好,所以我從來沒有嘗試過司機......所以對不起隊友,不能幫助你。 – skhro87

+0

更新了我的回答 – skhro87