Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 886
Amit Avatar asked Sep 25 '26 18:09

Amit


1 Answers

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.

like image 122
Feanor Avatar answered Sep 28 '26 08:09

Feanor