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?
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?
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
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
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.
Exception in thread "main" java.lang.IllegalArgumentException: dom.max_script_run_time must be == 0 || >= 30
So, minimum is 30 seconds or 0.
There were a few possible solutions to this over on Stackoverflow on Stop browser load from selenium webdriver.
This included:
Here is a function you may call to set the page load timeout you need.
I use three steps with chrome and it works:
We can use windows.stop();
in order to stop the page loading. Try the below code:
driver.execute_script("window.stop();")
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)