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

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
Amit
  • 219
  • 2
  • 7
  • 14

2 Answers2

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.

Feanor
  • 2,715
  • 4
  • 29
  • 43
  • 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 – Amit May 02 '12 at 07:54
  • 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. – Feanor May 02 '12 at 16:07
  • 1
    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 .. – Amit May 03 '12 at 07:12
  • Have you traced back through the source to where the exception is being thrown? – Feanor May 03 '12 at 17:13
  • 1
    yah, when we are going to set pageLoadTimeout() – Amit May 10 '12 at 06:23
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.

Community
  • 1
  • 1
Nashibukasan
  • 2,028
  • 23
  • 37