我正在使用Selenium 2测试(用C#编写),从"选择"控件中选择值.选择会导致回发到服务器,从而更新页面状态.因此,thread.sleep
在选择等待页面更改的值后,我执行手动wait().它可以与Thread.Sleep一起使用.但是,Thread.Sleep
使用有很多正当理由是一个坏主意,所以当我拿出我Thread.Sleep
的所有代码行时,我的所有测试用例都崩溃了,我尝试过WebDriverWait,隐式和明确地没有工作,非常沮丧
下面是我试过的示例代码....
// WebDriverWait
public IWebElement WaitForElement(By by)
{
// Tell webdriver to wait
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.PollingInterval = TimeSpan.FromSeconds(2);
wait.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(NoSuchFrameException));
wait.IgnoreExceptionTypes(typeof(StaleElementReferenceException), typeof(StaleElementReferenceException));
IWebElement myWait = wait.Until(x => x.FindElement(by));
return myWait;
}
Run Code Online (Sandbox Code Playgroud)
试过这个:
WebDriverWait wait = new WebDriverWait(new SystemClock(), driver, TimeSpan.FromSeconds(30), TimeSpan.FromMilliseconds(100));
Run Code Online (Sandbox Code Playgroud)
//隐式:
driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
Run Code Online (Sandbox Code Playgroud)
//显式等待:
IWebDriver driver = new FirefoxDriver();
driver.Url = "http://somedomain/url_that_delays_loading";
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
{
return …
Run Code Online (Sandbox Code Playgroud)