2012-08-14 43 views
2

我遇到了問題。我的網頁有一個DropDownList控件。一旦DropDownList值改變(通過選擇不同的值),頁面刷新並呈現內容。什麼是使用Webdriver使用Selenium的最佳方法

然後我必須使用Thread.Sleep(2000);纔去FindElement

我的問題:什麼是等待頁面加載的最佳方式?

我的代碼中有很多Thread.Sleep(2000)的實例,我開始認爲這不是解決問題的最佳方法。

這裏是我的代碼:

[TestInitialize()] 
public void Setup() 
{ 
    if (BaseIntegrationTest.browserType.Equals(BaseIntegrationTest.IE)) 
    { 
     driver = new InternetExplorerDriver(); 
    } 
    else if (BaseIntegrationTest.browserType.Equals(BaseIntegrationTest.CHROME)) 
    { 
     //driver = new ChromeDriver(); 
    } 
    else if (BaseIntegrationTest.browserType.Equals(BaseIntegrationTest.FIREFOX)) 
    { 
     driver = new FirefoxDriver(); 
    } 
} 

,第二部分:

[TestMethod] 
public void testVerifyData() 
{ 
    // ................... 
    // ................... 
    driver.FindElement(By.XPath("//*[@id='ctl00_NavigationControl1_lnke']")).Click(); 

    Thread.Sleep(2000); 

    //select from the dropdownlist. 
    IWebElement catagory = driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_Filter")); 
    SelectElement selectCatagory = new SelectElement(catagory); 
    selectCatagory.SelectByText("Employee"); 

    Thread.Sleep(2000); 
    // ................... 
    // ................... 
} 

回答

7

Thread.sleep()方法是實現您的等待很氣餒方式

此代碼概述了硒文檔http://seleniumhq.org/docs/04_webdriver_advanced.html

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
IWebElement category = wait.Until<IWebElement>((d) => 
    { 
     return d.FindElement(By.Id("ctl00_ContentPlaceHolder1_Filter")); 
    }); 

這是一個明確的等待的例子,其中硒不會直到你的元素被發現

執行任何動作

隱式等待的一個例子是:

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10)); 
IWebElement category = driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_Filter")); 

在隱式等待中,驅動程序將等待一定的時間並輪詢DOM以查找不存在的任何元素。

編輯

public WaitForElement(string el_id) 
{ 
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
    IWebElement category = wait.Until<IWebElement>((d) => 
    { 
     return d.FindElement(By.Id(el_id)); 
    }); 
} 
+0

感謝格雷格,所以我必須爲每個ID做?或者有什麼辦法可以讓我變得更抽象? – 2012-08-15 22:21:06

+0

Np,如果你喜歡它,接受答案XD。只要將它作爲一個對象,如果你想重新調用它,那麼你可以只提供一個id作爲參數或任何東西,並在需要時重用它。這就是我所做的一切等待 – Greg 2012-08-15 23:23:24

+0

你能告訴我你是怎麼做的嗎? – 2012-08-17 02:01:03

0

假設由相互作用的刷新是什麼,硒能檢測,那麼下面的方法可能會做你需要什麼:

waitForPageToLoad

據我所知,這個方法檢查瀏覽器中的一個狀態,看看瀏覽器是否認爲它正在加載內容,並等待直到這個狀態被清除。然而,這種就緒狀態有些陳舊 - 有很多方法可以動態加載頁面或頁面的一部分,因此就緒狀態並不總是準確描述發生了什麼。

如果您知道由於您的操作會發生什麼變化 - 也就是說,如果您知道在與下拉列表進行交互之後,會出現以前不在頁面上的特定UI元素 - 那麼您可能會更好地等待這些特定的UI元素出現。您可以通過編寫一個方法來重複檢查isTextPresent並以小增量等待,直到達到超時或出現預期元素。還有其他的「X Present」方法可用於這些方法,您可以在Selenium界面I的其他地方找到鏈接到的方法。

+0

waitForPageToLoad不是硒中的api 2 – Greg 2012-08-15 21:59:22

+0

哎呀。我在很久之後再次跳入Selenium。有很多要學習。 :) 謝謝! – 2012-08-16 13:08:33

0

,如果你想獲取HTML頁面加載或超時過期後:

using (var chromeDriver = new ChromeDriver()) 
     { 
      chromeDriver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(30)); 

      try 
      { 
       chromeDriver.Navigate().GoToUrl(link); 
       return chromeDriver.PageSource; 
      } 
      catch (WebDriverTimeoutException) 
      { 
       return chromeDriver.PageSource; 
      } 
     } 
1

我用WebDriverWait過解決這個問題。

但我設置了等待直到最後顯示的元素(例如:頁腳,列表中的最後一項)。

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
wait.Until(d => d.FindElement(By.Id("footer")).Displayed); 

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
wait.Until(d => d.FindElements(By.TagName("li")).Last().Displayed); 
0

我做了一個自定義函數爲我自己,也許這將是有用的人:

public static void waitForPageToBeLoaded(WebDriver driver, int time) { 

    ExpectedCondition<Boolean> expectation = new 
    ExpectedCondition<Boolean>() {  
     public Boolean apply(WebDriver driver) { 
      return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete"); 
      } 
    }; 

    Wait<WebDriver> wait = new WebDriverWait(driver, Long.valueOf(time)); 
    try { 
     wait.until(expectation); 
    } catch(Throwable error) { 
     System.out.println(error); 
     mainFunctions.reportFailure("[waitForPageToBeLoaded] Page load took longer than " + time/60 + " min."); 
    } 
} // waitForPageLoaded 
0

有些情況下,你需要等待,在這種情況下,Task.Delay方法將提供比Thread.Sleep更可預測的結果。

Task.Delay(1000).Wait (); //等待1秒

相關問題