加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 我怎样才能让Selenium-WebDriver在sendkey之后等待几秒钟

发布时间:2020-12-15 08:34:06 所属栏目:百科 来源:网络整理
导读:我正在研究C#Selenium-WebDriver.发送密钥后,我想等几秒钟.我执行以下代码等待2秒钟. public static void press(params string[] keys){ foreach (string key in keys) { WebDriver.SwitchTo().ActiveElement().SendKeys(key); Thread.Sleep(TimeSpan.FromSe
我正在研究C#Selenium-WebDriver.发送密钥后,我想等几秒钟.我执行以下代码等待2秒钟.
public static void press(params string[] keys)
{
       foreach (string key in keys) 
       { 
          WebDriver.SwitchTo().ActiveElement().SendKeys(key);
          Thread.Sleep(TimeSpan.FromSeconds(2));
       }
}

我称之为:

press(Keys.Tab,Keys.Tab,Keys.Tab);

它工作正常.哪一种更好?

解决方法

我会不惜一切代价避免使用类似的东西,因为它会减慢测试速度,但我遇到了一个我没有其他选择的情况.
public void Wait(double delay,double interval)
{
    // Causes the WebDriver to wait for at least a fixed delay
    var now = DateTime.Now;
    var wait = new WebDriverWait(myWebDriver,TimeSpan.FromMilliseconds(delay));
    wait.PollingInterval = TimeSpan.FromMilliseconds(interval);
    wait.Until(wd=> (DateTime.Now - now) - TimeSpan.FromMilliseconds(delay) > TimeSpan.Zero);
}

以某种方式观察DOM总是更好,例如:

public void Wait(Func<IWebDriver,bool> condition,double delay)
{
    var ignoredExceptions = new List<Type>() { typeof(StaleElementReferenceException) };
    var wait = new WebDriverWait(myWebDriver,TimeSpan.FromMilliseconds(delay)));
    wait.IgnoreExceptionTypes(ignoredExceptions.ToArray());
    wait.Until(condition);
}

public void SelectionIsDoneDisplayingThings()
{
    Wait(driver => driver.FindElements(By.ClassName("selection")).All(x => x.Displayed),250);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读