How to pause the execution of selenium webdriver code in java

2,198 views
Skip to first unread message

Rajesh Sharma

unread,
Jan 11, 2015, 11:22:26 AM1/11/15
to webd...@googlegroups.com
Hi Everyone, I am stuck in a scenario.
I need to select a radio button and then select a value from the dropdown. Problem is that upon selecting radio button page reloads in backend which takes around 5 seconds and then value in the dropdown appears based on the radio button selected. My script selects the radio button and then immediately look for the value in dropdown which is the reason my script fails. I have tried using thread.sleep, implicit and explicit wait but nothing works.

Any Suggestions with sample code would b greatly appreciated

Selenium Framework

unread,
Jan 12, 2015, 2:02:41 PM1/12/15
to webd...@googlegroups.com
Why wouldn't Thread.sleep(5000) work ?

darrell

unread,
Jan 12, 2015, 11:24:34 PM1/12/15
to webd...@googlegroups.com
Think about how you would test this manually. What are you waiting for when you select a value for the dropdown? If I know the drop down will change I could do the following:

    // get the values on the drop down
    // select the radio button
    // wait until the drop down changes

Or if the dropdown has no options until you select a radio button then you can do:

    // select the radio button
    int size = 0;
    int maxMilliseconds = 30000;
    do {
        size = driver.findElements(By.cssSelector("put something here which gets all the dropdown options")).size();
        sleepTight(200);
        maxMilliseconds -= 200;
    } while(size < 1 && maxMilliseconds > 0);

This will get the number of options on the drop down then wait 200ms. If the number of options becomes greater than zero then it leaves the loop. I put in a safeguard so it won't wait longer than 30 seconds as well.

You just have to be sure what the code is looking for is the right thing to wait for. Another option is to look at WebDriverWait. The way it would work would be:

    WebDriverWait wdw = new WebDriverWait(driver, 30);
    // select the radio button
    wdw.until(ExpectedConditions.presenceOfElementLocatedBy(By.cssSelector("something which locates the dropdown option you are hoping for")));

This will wait for up to 30 seconds for the element to appear. This assumes you are waiting for the element to appear. 

Øystein

unread,
Jan 13, 2015, 2:41:16 AM1/13/15
to webd...@googlegroups.com

A even better question would be: why would not explicit wait work ? J

 

But back to the point. I don’t think you are dealing with a proper select element. For us to help you, you probally need to show more html at this time.

 

From: webd...@googlegroups.com [mailto:webd...@googlegroups.com] On Behalf Of Selenium Framework
Sent: 12. januar 2015 20:03
To: webd...@googlegroups.com
Subject: [webdriver] Re: How to pause the execution of selenium webdriver code in java

 

Why wouldn't Thread.sleep(5000) work ?

On Sunday, January 11, 2015 at 11:22:26 AM UTC-5, Rajesh Sharma wrote:

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.

sunny sachdeva

unread,
Jan 14, 2015, 4:15:19 AM1/14/15
to webd...@googlegroups.com
You need to poll until the drop down list size > 0 as clearly mentioned by Darell.

Thanks
Sunny
Reply all
Reply to author
Forward
0 new messages