13

I searched online, but couldn't find anything about how to stop page loading over a browser using selenium web-driver.

Any idea for the same?

Suchit Parikh
1,5821 gold badge10 silver badges22 bronze badges
asked Feb 4, 2013 at 18:24
2
  • Can you explain what you mean by 'stop page loading' ? Commented Feb 4, 2013 at 18:51
  • 4
    he wants the page to stop loading... Commented Feb 4, 2013 at 19:13

8 Answers 8

11

This might be useful.

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

OR

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

You must use driver.page_load_strategy = 'none' if using chrome else chromedriver will wait for full url to load before performing execution

answered Jul 4, 2013 at 12:56
1
  • The find element body did not work for me as it could not find the element until the page loaded. However the Javascript works beautifully Commented Aug 7, 2017 at 10:28
5

If you want to simulate the browser's STOP button, this post should help you out https://stackoverflow.com/questions/5453423/how-to-stop-the-page-loading-in-firefox-programaticaly

answered Feb 4, 2013 at 19:13
1
  • yeah! I have gone through that post earlier,but couldn't understand how to really write it... as that was shorthand code. can you help me on that? Commented Feb 5, 2013 at 18:10
5

If you are using firefox then you can set preference for default timeout:

fp = webdriver.FirefoxProfile()
fp.set_preference("http.response.timeout", 5)
fp.set_preference("dom.max_script_run_time", 5)
driver = webdriver.Firefox(firefox_profile=fp)
driver.get("http://www.google.com/")

This will stop page load after 5 seconds.

answered Apr 29, 2015 at 23:17
1
  • I get: Exception in thread "main" java.lang.IllegalArgumentException: dom.max_script_run_time must be == 0 || >= 30 So, minimum is 30 seconds or 0. Commented Aug 1, 2019 at 14:16
1

Here is a function you may call to set the page load timeout you need.

answered Jun 7, 2013 at 5:24
2
  • Link only answers are not encouraged. Please add the solution part of the link to your answer. Commented Apr 30, 2015 at 11:43
  • 1
    Also, the link is dead. Commented Feb 9, 2017 at 11:57
0

I use three steps with chrome and it works:

  1. Navigate to "about:blank"
  2. Get element "body"
  3. On that element send keys "Esc"
IAmMilinPatel
7,7667 gold badges44 silver badges68 bronze badges
answered Aug 24, 2016 at 13:57
0

We can use windows.stop(); in order to stop the page loading. Try the below code:

driver.execute_script("window.stop();")
alecxe
11.4k11 gold badges52 silver badges107 bronze badges
answered Feb 12, 2019 at 17:18
0

The .execute_script() method didn't work for me as it turned out the specified script was executing on an about:blank page before it visited the url. What did work for me was injecting the script into the chromedriver session itself (such that the script runs for every page load) inspired from this SO answer.

driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {'source': 'setTimeout(function(){window.stop();}, 5000);'})
driver.get(urls)
answered Feb 6, 2024 at 6:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.