9

I am creating a test and having some issues. Here is the scenario. I use Selenium Web driver to fill out a form on Page1 and submit the form by clicking a button. Page2 starts loading... but the problem is, Page2 uses Google Analytics codes, and sometimes it takes forever for the page to stop loading.

Even though the expected element is already present, Selenium web driver does not proceed until the whole web page is fully loaded.

How do I make Selenium to move on to the next task or stop loading external javascript/css if the expected element is already present?

I tried tweaking the following settings but no luck.

driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

TEMPORARY SOLUTION: Scroll below for answer!

roosevelt
  • 1,874
  • 4
  • 20
  • 27

7 Answers7

8

Give below approaches a shot.

driver.findElement(By.tagName("body")).sendKeys("Keys.ESCAPE");

or

((JavascriptExecutor) driver).executeScript("return window.stop");

Alternatively, you can also use WebDriverBackedSelenium as shown in the snippet below from Vincent Bouvier.

//When creating a new browser:
WebDriver driver = _initBrowser(); //Just returns firefox WebDriver
WebDriverBackedSelenium backedSelenuium = 
            new WebDriverBackedSelenium(driver,"about:blank");    

//This code has to be put where a TimeOut is detected
//I use ExecutorService and Future<?> Object

void onTimeOut()
{
    backedSelenuium.runScript("window.stop();");
}

Source: https://sqa.stackexchange.com/a/6355

Source: https://stackoverflow.com/a/13749867/330325

Community
  • 1
  • 1
Buddha
  • 4,339
  • 2
  • 27
  • 51
  • I gave it a shot but same issue remains. Whether I click the button by JavascriptExecutor or WebDriver, Page2 starts to load. But since Page2 is not completely loaded, it either throws a pagetimeout error or doesn't move to the next statement (which is to check if expected elm is present in Page2). Page2 has the element visitble, it's just selenium is adamant to only continue after the page is completely loaded. – roosevelt Jan 19 '14 at 17:13
  • FYI, I also tried try/catch the pagetimeout exception to see if I could still execute selemium commands but it doesn't execute the commands either :( – roosevelt Jan 19 '14 at 18:37
5

So, I reported to Selenium about these issues. And the temporary workaround is... messing with Firefox's timeout settings. Basically by default Firefox waits about 250 seconds for each connection before timing you out. You can check about:config for the details. Basically I cranked it down so Firefox doesn't wait too long and Selenium can continue as if the page has already finished loading :P.

Similar config might exist for other browsers. I still think Selenium should let us handle the pagetimeout exception. Make sure you add a star to the bug here: http://code.google.com/p/selenium/issues/detail?id=6867&sort=-id&colspec=ID%20Stars%20Type%20Status%20Priority%20Milestone%20Owner%20Summary, so selenium fixes these issues.

FirefoxBinary firefox = new FirefoxBinary(new File("/path/to/firefox.exe"));
FirefoxProfile customProfile = new FirefoxProfile();
customProfile.setAcceptUntrustedCertificates(true);
customProfile.setPreference("network.http.connection-timeout", 10);
customProfile.setPreference("network.http.connection-retry-timeout", 10);

driver = new FirefoxDriver(firefox, customProfile);
driver.manage().deleteAllCookies();
roosevelt
  • 1,874
  • 4
  • 20
  • 27
0

Once you have checked for the element and you know that it is present, you could either navigate to/load a different page (if the next tasks are on a different page) or if the tasks are on the same page (as you anyway do not need the elements that have not yet loaded), you could continue as usual - selenium will identify the elements which have already been loaded. This works for me when I work with feature rich pages.

ucsunil
  • 7,378
  • 1
  • 27
  • 32
0

Instead of using the webdriver click() to submit the form use jsexecutor and do a click. Jsexecutor does not wait for page load and you can with other actions.

Akbar
  • 1,513
  • 4
  • 20
  • 34
  • I tried using `((JavascriptExecutor)getDriver()).executeScript("document.getElementById('btn-id').click();");` but same problem. Page2 is loading, Page2 already has the item loaded but since the page is not completely finished loading, Selenium is not executing the next command `getDriver().findElement(By.id("elm-waiting-for")).isDisplayed()` – roosevelt Jan 19 '14 at 17:06
-1

As per the above scenario explained i feel its best to use the below wait command in the first page.

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id(>someid>)));

Once the required element is found in the first page next you can proceed to the second page.

ndpu
  • 22,225
  • 6
  • 54
  • 69
-1

As per the above scenario explained i feel its best to use the below wait command in the first page.

WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = 
        wait.until(ExpectedConditions.presenceOfElementLocated(By.id(>someid>)));

Once the required element is found in the first page next you can proceed to the second page.

JMax
  • 26,109
  • 12
  • 69
  • 88
-2

Use explicit/webdriver wait----

   WebDriverWait wt=new WebDriverWait(driver, 20);
   wt.until(ExpectedConditions.elementToBeClickable(By.name("abc")));
SATYA
  • 52
  • 3