What is the best approach for Timeout using Selenium using Webdriver

19,013

Solution 1

Thread.Sleep() is a very discouraged way to implement your waits

This code is outlined on the selenium documentation 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"));
    });

That is an example of an explicit wait where selenium will not execute any actions until your element is found

An example of an implicit wait is:

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

In implicit waits the driver will wait for a given amount of time and poll the DOM for any elements that do not exist.

EDIT

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));
    });
}

Solution 2

I solve this problem by using WebDriverWait too.

But I set wait till the last element shown up (ex: the footer, the last item in the list).

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

or

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

Solution 3

There are some cases where you do need to wait, in which case the Task.Delay method will provide more predictable results than Thread.Sleep

Task.Delay(1000).Wait(); // Wait 1 second

Share:
19,013
Nick Kahn
Author by

Nick Kahn

Updated on June 28, 2022

Comments

  • Nick Kahn
    Nick Kahn almost 2 years

    I have run into a problem. My web page has a DropDownList control. Once the DropDownList value changes (by selecting a different value), the page refreshes and it renders the contents.

    And then I have to use Thread.Sleep(2000); before it goes and FindElement.

    My question: What is the best way to wait till the page loads?

    I have so many instances of Thread.Sleep(2000) in my code that I am beginning to think this is not the best way to approach the problem.

    Here is my code:

    [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();
        }
    }
    

    And the second part:

    [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);
        // ...................
        // ...................
    }