Amit
Amit

Reputation: 229

how to give wait to driver.get() explicitly in selenium using java

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

Upvotes: 3

Views: 4970

Answers (2)

Nashibukasan
Nashibukasan

Reputation: 2028

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.

Upvotes: 0

Feanor
Feanor

Reputation: 2725

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.

Upvotes: 5

Related Questions