How to combine implicit and explicit timeouts in Selenium?

前端 未结 1 1030
萌比男神i
萌比男神i 2020-11-28 16:36

I am using Selenium ChromeDriver with an implicit timeout:

_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);

In one of my

相关标签:
1条回答
  • 2020-11-28 17:13

    As per the documentation of Explicit and Implicit Waits it is clearly mentioned that:

    Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an implicit wait of 10 seconds and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds.

    Still if you have implicit timeout defined as:

    _driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
    

    Before inducing explicit wait for the element to be found you need to remove the implicit timeout as follows:

    _driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(0);
    WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(120));
    wait.Until(d => d.FindElement(By.CssSelector("div.example")));
    

    Once you are done with the explicit wait, you can re-configure back the implicit timeout again as:

    _driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(0);
    WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(120));
    wait.Until(d => d.FindElement(By.CssSelector("div.example")));
    //perform your action with the element
    _driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
    
    0 讨论(0)
提交回复
热议问题