Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium + Python: How to stop page loading when certain element gets loaded?

Tags:

The implicit and explicit waits can be used when the page uses AJAX, but I want to stop the loading caused by driver.get() when sufficient elements are loaded. Is it possible to do so because of the driver.get() call returns only when the page finishes loading.

like image 893
RamKumar Avatar asked Jun 12 '17 15:06

RamKumar


People also ask

What is page load timeout?

This defines the amount of time that Selenium will wait for a page to load. By default, it is set to 0 (which equates to an infinite time out). If you want to ensure that an error is thrown if your page takes longer than expected to load, you can modify this by using this line of code: driver.

How do I stop a Selenium session?

close() and driver. quit() are two methods for closing a browser session in Selenium WebDriver.

How do you wait for a page to load in selenium?

There are three ways to implement Selenium wait for page to load: The Implicit Wait tells WebDriver to wait a specific amount of time (say, 30 seconds) before proceeding with the next step. If the tester knows how much time the page and element will take to load, they should use Implicit Wait.

How to stop page load in selenium with Chrome extension?

Installing an extension in Chrome called Stop load using the built in pageLoadTimeout() method in Selenium using Sikuli or Autoit to access the browser's native controls

What is page load timeout in Selenium WebDriver?

Wait time for page load time – set_page_load_timeout (self, time_to_wait) is used to specify the maximum wait time (in seconds) for a page to load completely in a selenium WebDriver controlled browser. This is useful when you are performing Selenium automation testing in a throttling network condition.

How to stop a page from loading in JavaScript?

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.


1 Answers

Yes it's possible by setting the pageLoadStrategy capability to none. Then wait for an element to be present and call window.stop to stop the loading:

from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By  capa = DesiredCapabilities.CHROME capa["pageLoadStrategy"] = "none"  driver = webdriver.Chrome(desired_capabilities=capa) wait = WebDriverWait(driver, 20)  driver.get('http://stackoverflow.com/')  wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#h-top-questions')))  driver.execute_script("window.stop();") 
like image 200
Florent B. Avatar answered Oct 23 '22 10:10

Florent B.