3

how to give wait to driver.get(), because the URL we are visiting using .get() is dont know. and may take unkown time so is we have to give 30seconds timeout to diver.get() then how to give it.

following is the code for it..

package org.openqa.selenium.example;

import java.util.List;

import org.openqa.selenium.By

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;


public class MyClass{

    public static void main(String[] args) throws Exception {

        // The Firefox driver supports javascript 

        WebDriver driver = new HtmlUnitDriver();

        // Go to the some websites

        driver.get("http://www.abced.com/123456/asd.htm");

        /***  Here we DONT get back the driver, so we need to Give Time out of 30 seconds**/

        final List<WebElement> element1= driver.findElements(By.tagName("a"));

    for (WebElement webElement : element1) {

        String urlFromPage = webElement.getAttribute("href");

                System.out.println(urlFromPage);

        }


     }

}

I tried

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(thisPage.url);

It is not working.. plz suggest, tx

2 Answers 2

5

If you want to wait for the page to load, you should instead use the pageLoadTimeout(long time, java.util.concurrent.TimeUnit unit) method. The implicitlyWait(long time, java.util.concurrent.TimeUnit unit) is used to wait for elements that haven't appeared yet, rather than waiting for the page to load.

On your WebDriver instance, you should call a similar method chain to the one you used with implicitlyWait(). This will call, in order:

  • manage() - the driver management interface
  • options() - the driver options interface
  • timeouts() - the timeout options interface
  • pageLoadTimeout(...) - setting the timeout to the time you want

You can find the relevant javadoc here.

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

5 Comments

Thanks for quick reply. I dont know how to use it.. So will you please tell me, how to use pageLoadTimeout() with the driver.. I am using selenium 16.0
Edited with a little more detail. If you need more help, please edit your question to make it more clear what you are trying to do.
I tried same but when I am executing program then Iam getting Exception : " java.lang.UnsupportedOperationException: pageLoadTimeout SO, I am not able to work on this ..
Have you traced back through the source to where the exception is being thrown?
yah, when we are going to set pageLoadTimeout()
0

Instead you can use WebDriverWait to specify a condition to check and a maximum timeout. This can be used as follows:

WebDriverWait _wait = new WebDriverWait(driver, new TimeSpan(0, 0, 2)); //waits 2 secs max
_wait.Until(d => d.FindElement(By.Id("name")));

I have posted a similar answer to this question.

Comments

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.