隐藏

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

发布:2020/9/25 9:57:20作者:管理员 来源:本站 浏览次数:1148

我正在研究C#Selenium-WebDriver.发送密钥后,我想等几秒钟.我执行以下代码等待2秒钟.
			
  1. public static void press(params string[] keys)
  2. {
  3. foreach (string key in keys)
  4. {
  5. WebDriver.SwitchTo().ActiveElement().SendKeys(key);
  6. Thread.Sleep(TimeSpan.FromSeconds(2));
  7. }
  8. }

我称之为:

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

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

解决方法

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

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

			
  1. public void Wait(Func<IWebDriver,bool> condition,double delay)
  2. {
  3. var ignoredExceptions = new List<Type>() { typeof(StaleElementReferenceException) };
  4. var wait = new WebDriverWait(myWebDriver,TimeSpan.FromMilliseconds(delay)));
  5. wait.IgnoreExceptionTypes(ignoredExceptions.ToArray());
  6. wait.Until(condition);
  7. }
  8.  
  9. public void SelectionIsDoneDisplayingThings()
  10. {
  11. Wait(driver => driver.FindElements(By.ClassName("selection")).All(x => x.Displayed),250);
  12. }