WebDriverWait或ImplicitlyWait或ExplictlyWait无效 [英] WebDriverWait or ImplicitlyWait or ExplictlyWait nothing works

查看:555
本文介绍了WebDriverWait或ImplicitlyWait或ExplictlyWait无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium 2测试(用C#编写),该测试从选择"控件中选择值.选择会导致回发到服务器,从而更新页面的状态.因此,我在选择一个值以等待页面更改后执行手动等待(thread.sleep).并且它与Thread.Sleep一起正常工作.但是,出于很多原因使用Thread.Sleep是一个坏主意,因此当我取出所有Thread.Sleep行代码时,我的所有测试用例都崩溃了,我尝试了WebDriverWait,隐式和显式地都没有用,并且非常沮丧

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

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

推荐答案

这对我有用->

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

在没有看到更多代码的情况下,很难确定为什么它不起作用.

Without seeing more of your code, it will be hard to determine why it's not working.

这篇关于WebDriverWait或ImplicitlyWait或ExplictlyWait无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆