1

I'm using selenium/firefox/c# to enter data into several fields in a webpage, and then submit it to the website by clicking a submit button. I have tried using ImplicitlyWait so that the program waits for the results page to load for a maximum of 45 seconds and then grab results from that page. I have it currently coded as this

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(45));
driver.FindElement(By.Id("dnn_ctr1734_Professional_btnSubmit")).Click();

I'm putting the implicit wait before clicking the submit button, but even if it takes only 15 seconds for the page to load its not grabbing the results when it finally loads. Do I have the ImplicitWait in the right order?

1
  • Nope, 45 sec is a long time. When that happen it means something else is going on. Make sure that the button is visible. Do a screen capture by code. Also maximize the window before clicking. Sometimes the problem is just that the default size for the window is too small and element is not visible. What browser are you using? Commented Apr 13, 2016 at 16:21

3 Answers 3

4

The correct place to put an implicit wait is usually right after you initialize the driver. It basically acts as a default wait time for an action to complete.

There are several ways to solve your problem...

  1. Use an Explicit wait with an expected condition to wait until your busy spinner is gone.

    var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10)); wait.Until(driver => !driver.FindElement(By.Id("busySpinnerID")).Displayed);

  2. Wait until a particular element is visible on the page

    var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10)); wait.Until(driver => driver.FindElement(By.Id("knownElementId")).Displayed);

  3. Use JavaScript to wait until the page has loaded

    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30)); wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));

Sign up to request clarification or add additional context in comments.

Comments

1

I agree with Dmitry, the best way to deal with waits is with the explicit wait function. To make your life even easier, you can add this into an extension method so you're always going to be waiting for the element to exist before clicking. Example:

Class Actions {

        public static IWebDriver ClickOn(IWebDriver driver, string button) 
        {
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
            wait.Until(ExpectedConditions.ElementExists(By.XPath(button)));
            driver.FindElement(By.XPath(button)).Click();
            return driver;
        }
}

Then your code can be as simple as

Actions.ClickOn(d, SignInButton);

And your code will still wait for the element to exist before clicking.

Comments

0

You should use explicit wait, use code like below:

 new WebDriverWait(driver, TimeSpan.FromSeconds(45)).Until(ExpectedConditions.ElementExists((By.Id("dnn_ctr1734_Professional_btnSubmit"))));
 driver.FindElement(By.Id("dnn_ctr1734_Professional_btnSubmit")).Click();

3 Comments

I'm using firefox. I should re-iterate that in this project the submit button is always present and available to click. What I want to do is after I click the submit button I want the page to wait until the results load, and then look for other elements that always appear after the page loads. Should I put the implicit wait after driver.FindElement(By.Id("dnn_ctr1734_Professional_btnSubmit")).Click();
Here is more of the code so you can see what I'm looking for. Once I click the submit button it can take anywhere between 5-45 seconds for the results page to load (there is a spinning logo that appears after clicking submit until it loads).
driver.FindElement(By.Id("dnn_ctr1734_Professional_btnSubmit")).Click(); if (driver.FindElements(By.XPath(".//tr[td[contains(text(),'Claim Status')]]/td[2]")).Count > 0) { activeSheet.Range["P" + n].Value = driver.FindElement(By.XPath(".//tr[td[contains(text(),'Claim Status')]]/td[2]")).Text; activeSheet.Range["Q" + n].Value = driver.FindElement(By.XPath(".//tr[td[contains(text(),'Claim ICN')]]/td[2]")).Text; activeSheet.Range["R" + n].Value = driver.FindElement(By.XPath(".//tr[td[contains(text(),'Paid Amount')]]/td[2]")).Text; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.