Selenium C# WebDriver: Wait until element is present

前端 未结 24 3177
礼貌的吻别
礼貌的吻别 2020-11-22 08:53

I want to make sure that an element is present before the webdriver starts doing stuff.

I\'m trying to get something like this to work:

WebDriverWait w         


        
24条回答
  •  旧巷少年郎
    2020-11-22 09:51

    WebDriverWait won't take effect.

    var driver = new FirefoxDriver(
        new FirefoxOptions().PageLoadStrategy = PageLoadStrategy.Eager
    );
    driver.Navigate().GoToUrl("xxx");
    new WebDriverWait(driver, TimeSpan.FromSeconds(60))
        .Until(d => d.FindElement(By.Id("xxx"))); // a tag that close to the end
    

    This would immediately throw an exception once the page is "interactive". I don't know why but the timeout acts as if not exist.

    Perhaps SeleniumExtras.WaitHelpers works but I didn't try. It's official but was split out into another nuget package. You can refer to C# Selenium 'ExpectedConditions is obsolete'.

    Myself is using FindElements and check whether Count == 0, if true, use await Task.Delay. It's really not quite efficient.

提交回复
热议问题