我正在使用Selenium 2测试(用C#编写),它从“选择”控件中选择值。选择会导致回发到服务器,从而更新页面状态。因此,在选择等待页面更改的值后,我正在执行手动等待(thread.sleep
)。它可以与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;
}
也试过这个:
WebDriverWait wait = new WebDriverWait(new SystemClock(), driver, TimeSpan.FromSeconds(30), TimeSpan.FromMilliseconds(100));
//隐式:
driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
//显式等待:
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 d.FindElement(By.Id("someDynamicElement"));
});
答案 0 :(得分:1)
这对我有用 - &gt;
WebDriverWait wait = new WebDriverWait(browser, new TimeSpan(0, 0, 30));
element = wait.Until<IWebElement>((driver) =>
{
return driver.FindElement(By.Name("name_of_element")));
});
你也可以通过ID - &gt;
来做WebDriverWait wait = new WebDriverWait(browser, new TimeSpan(0, 0, 30));
element = wait.Until<IWebElement>((driver) =>
{
return driver.FindElement(By.Id("id_of_element")));
});
如果没有看到更多的代码,就很难确定它为什么不起作用。
答案 1 :(得分:0)
尝试使用
new WebDriverWait(driver, 30).until(ExpectedConditions.presenseOfElementLocated(byLocator));
答案 2 :(得分:0)
我找到了stackoverflow的解决方案:)这样可行:
click on partialLinkText("Exit")
remote.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS)
remote.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS)
// Thread.sleep(7000) // for js-work
(new WebDriverWait(remote, 245)).until(presenceOfElementLocated(By.partialLinkText("""Entry for > technician""")))
// Thread.sleep(3000) // for js-works