selenium and firefox timeout behavior

Posted by Md. Reazul Karim on Stack Overflow See other posts from Stack Overflow or by Md. Reazul Karim
Published on 2012-11-27T05:02:55Z Indexed on 2012/11/27 5:03 UTC
Read the original article Hit count: 164

Filed under:
|
|

I am facing this strange timeout behavior. I tried to load some page using:

    WebDriver drv = new FirefoxDriver();
    drv.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    drv.get(url);
    String email = "";

    try {
        WebElement aElem = Util.safeFindElementByXpath(drv, "//p[1]/a[1]");

        if (aElem != null) {
            email = aElem.getAttribute("href");
        }
    } catch (Exception e) {
    }
    drv.quit();
    return email;

The safeFindElementByXpath is:

   static WebElement safeFindElementByXpath(WebDriver driver, String xpath) {
        try {
            return driver.findElement(By.xpath(xpath));             
        } catch (NoSuchElementException e) {
            return null;
        }
    }

Now I set the firefox network.http.connection-timeout and network.http.connection-retry-timeout both to 10. Now if I restart the firefox I can see the new values. But if I try to run the above code - the firefox window opens and it waits for a particular website for a long time. Hence I open another tab in the same firefox window and check the values of timeout and find them to be 90 and 250. If I change the values on that same window, the selenium code immediately finishes requesting for the page (ie it goes after the exception block).

So the thing is that the same code will work on many sites and I don't know beforehand which site is invalid/down and I was thinking of putting this code in a separate thread and kill the thread after some time from the calling thread. But that is not possible I guess because the child thread has no way to poll anything as it is stuck at page request and can't go to the polling code afterwards.

Now the thing is that I want any kind of hack/workaround to accomplish this: try getting the emails for good sites and if there are bad sites just give up after a certain period (press the panic button sorta thing).

Thanks

© Stack Overflow or respective owner

Related posts about java

Related posts about firefox