Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ask Selenium2 to finish loading a page on .Click before doing any assertions?

I would like to use the selenium2 c# webdriver to test our login mechanism. The following test passes if I run through it in debug mode and give the Click method some time. If I run it normally though the test fails with a NoSuchElementException.

    [Test]
    public void TestClickLogonLink()
    {
        _driver.Navigate().GoToUrl("http://nssidManager.local/");
        IWebElement loginElement = _driver.FindElement(By.Id("loginWidget"));
        IWebElement logonAnchor = loginElement.FindElement(By.LinkText("Log On"));

        logonAnchor.Click();

        IWebElement userNameTextBox = _driver.FindElement(By.Id("UserName"));

        Assert.IsNotNull(userNameTextBox);
    }

How do I need to tell Selenium to Wait until the logonAnchor.Click has finished loading the next page?

like image 339
willem Avatar asked Jan 20 '23 06:01

willem


1 Answers

Set the implicit wait like so:

driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0,30));

This will allow Selenium to block until the FindElement succeeds or times out.

like image 181
Mike Kwan Avatar answered Mar 16 '23 00:03

Mike Kwan