roosevelt
roosevelt

Reputation: 1974

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!

Upvotes: 9

Views: 26984

Answers (7)

user2886114
user2886114

Reputation: 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.

Upvotes: -1

SATYA
SATYA

Reputation: 52

Use explicit/webdriver wait----

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

Upvotes: -2

user3227421
user3227421

Reputation: 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.

Upvotes: -1

roosevelt
roosevelt

Reputation: 1974

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

Upvotes: 5

Akbar
Akbar

Reputation: 1525

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.

Upvotes: 0

ucsunil
ucsunil

Reputation: 7494

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.

Upvotes: 0

Buddha
Buddha

Reputation: 4476

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

Upvotes: 8

Related Questions