How can I ask the Selenium-WebDriver to wait for few seconds after sendkey?

前端 未结 3 876
礼貌的吻别
礼貌的吻别 2021-02-06 02:34

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(para         


        
相关标签:
3条回答
  • 2021-02-06 02:57

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

    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);
    }
    
    0 讨论(0)
  • 2021-02-06 03:01

    You can use Thread.Sleep(miliseconds) function. This stop your C# thread and selenium continue

    [TestMethod]
    public void LoginTest()
    {
        var loginForm = driver.FindElementByXPath("//form[@id='login-form']");
    
        if (loginForm.Enabled)
        {
            MainPageLogin(loginForm);
            CheckLoginForm();
        }
        else
        {
            CheckLoginForm();
        }
    
        **Thread.Sleep(5000);**
        Assert.AreEqual(driver.Url, "https://staging.mytigo.com/en/");
    }
    
    0 讨论(0)
  • 2021-02-06 03:04
    public static void press(params string[] keys)
    {
           foreach (string key in keys) 
           { 
              WebDriver.SwitchTo().ActiveElement().SendKeys(key);
              Sleep(2000);
           }
    }
    

    which serves the same

    0 讨论(0)
提交回复
热议问题