7

I'm using Selenium 2 tests (written in C#) that choose values from a "select" control. Selection causes a post-back to the server, which updates the state of the page. I am therefore performing a manual wait (thread.sleep) after choosing a value to wait for the page to be changed. and it works fine with Thread.Sleep. However, Thread.Sleep is a bad idea to use with number of good reasons so when I take out all my Thread.Sleep line of code then all my test cases fall apart and I have tried WebDriverWait, Implicitly and Explicitly none works and very frustration

below is the sample code that I have tried....

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

Tried this too:

   WebDriverWait wait = new WebDriverWait(new SystemClock(), driver, TimeSpan.FromSeconds(30), TimeSpan.FromMilliseconds(100));

//Implicitly:

driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));

//Explicit Wait:

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

3 回答 3

1

这对我有用->

WebDriverWait wait = new WebDriverWait(browser, new TimeSpan(0, 0, 30));

element = wait.Until<IWebElement>((driver) =>
  {
     return driver.FindElement(By.Name("name_of_element")));
  });

你也可以通过 ID 来做 ->

WebDriverWait wait = new WebDriverWait(browser, new TimeSpan(0, 0, 30));

element = wait.Until<IWebElement>((driver) =>
  {
     return driver.FindElement(By.Id("id_of_element")));
  });

如果不查看更多代码,就很难确定它为什么不起作用。

于 2012-10-22T18:00:53.050 回答
0

尝试使用

new WebDriverWait(driver, 30).until(ExpectedConditions.presenseOfElementLocated(byLocator));
于 2012-10-20T10:27:47.293 回答
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
于 2017-04-10T14:15:43.477 回答