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

how to give wait to driver.get (), because the URL we visit using .get () does not know. and it may take some time, so we have to give a timeout of 30 seconds for diver.get (), then how to provide it.

following 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);

Does not work. Plz suggest th

+3
source share
2 answers

If you want to wait for the page to load, you should use the method instead pageLoadTimeout(long time, java.util.concurrent.TimeUnit unit). implicitlyWait(long time, java.util.concurrent.TimeUnit unit)Used to wait for items that have not yet appeared, instead of waiting for the page to load.

In your WebDriver instance, you should call a similar chain of methods with the one you used with implicitlyWait(). This will cause to:

  • manage() -
  • options() -
  • timeouts() - -
  • pageLoadTimeout(...) - -

javadoc .

+4

WebDriverWait, . :

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

.

0

All Articles