我如何要求Selenium-WebDriver在sendkey之后等待几秒钟? [英] How can I ask the Selenium-WebDriver to wait for few seconds after sendkey?

查看:167
本文介绍了我如何要求Selenium-WebDriver在sendkey之后等待几秒钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#Selenium-WebDriver.发送密钥后,我要等待几秒钟.我执行以下代码以等待2秒钟.

I'm working on a C# Selenium-WebDriver. After send key, I want to wait few seconds. I do the following code to wait for 2 seconds.

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

工作正常.哪一种是更好的方法?

It works fine. Which one is a better way?

推荐答案

我会不惜一切代价避免使用类似的方法,因为这样会降低测试速度,但是我遇到了我没有其他选择的情况.

I would avoid at all cost using something like that since it slows down tests, but I ran into a case where I didn't had other choices.

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总是更好,例如:

It's always better to observe the DOM somehow, e.g.:

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

这篇关于我如何要求Selenium-WebDriver在sendkey之后等待几秒钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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