Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Selenium Webdriver Stop Loading the page if the desired element is already loaded?

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!

like image 281
roosevelt Avatar asked Jan 19 '14 07:01

roosevelt


People also ask

How to stop browser load from Selenium WebDriver?

There were a few possible solutions to this over on Stackoverflow on Stop browser load from selenium webdriver. Here is a function you may call to set the page load timeout you need. We can use windows.stop (); in order to stop the page loading. Try the below code:

How to wait until the page is completely loaded in selenium?

We can wait until the page is completely loaded in Selenium webdriver by using the JavaScript Executor. Selenium can run JavaScript commands with the help of the executeScript method.

What is wrong with Page2 in selenium?

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 to execute JavaScript commands in selenium with selenium?

Selenium can execute JavaScript commands with the help of the executeScript command. To stop a page loading, the command window.stop () is passed as a parameter to the executeScript method. Also, for the Chrome browser we have to configure the pageLoadStrategy to none value and wait for the web element to be available.


2 Answers

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

like image 111
Buddha Avatar answered Oct 16 '22 21:10

Buddha


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();
like image 35
roosevelt Avatar answered Oct 16 '22 21:10

roosevelt