30

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.

4
  • Now why would you do such thing, let's say you wait for a button to show. Once the button shows you do whatever you want and it doesn't wait for the page to fully load before it executes your commands unless you got a alterenative reason for this, it's useless. Commented Jun 12, 2017 at 16:07
  • @ElvirMuslic But the call to driver.get() returns only when the page finishes loading completely. It's like I want to programmatically click that button once the button is visible not waiting for the entire page to load. Commented Jun 12, 2017 at 16:22
  • Not really, driver.get() just makes the requests it doesn't Wait for the elements to load. If you say driver.something.click() it will execute it as soon as it gets any response about the page and if the element is not present it will throw an error. Meaning, 1.driver.get() waits for any response from the page (not all). 2. You can execute anything that you'd like and the page doesn't have to be fully loaded but it will load while you're performing those actions. 3. This can be used to save time and usage I guess in some manner (for big projects) Commented Jun 12, 2017 at 16:33
  • @ElvirMuslic As Florent's answer, the driver.get() doesn't wait for the page to load only when the page load strategy is set to none. Commented Jun 13, 2017 at 2:14

2 Answers 2

63

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();")
Sign up to request clarification or add additional context in comments.

5 Comments

If I wanted to execute this code locally what would I need to do locally? I understand the dependency requirements but would I need to point to chromedriver within the above test? How would you execute the above without a language binding?
@Wunderbread, I'm not sure what you mean by "without a language binding", but to execute the code locally, either place the chromedriver in the PATH environment variable or provide the path to the constructor (webdriver.Chrome).
Got it, thanks. What I meant is executing the above test without a framework e.g. the command "pytest test_foo.py"
I am using lower version of Firefox so instead of page load strategy i did profile.setPreference("webdriver.load.strategy", "unstable"); which solved my problem 1 that is get returns even before page loading complete.But I got the problem that finding elemet begins only when page load is complete so I thought of sending escape key via action chains but was not able to do it and window.stop(); didnt work either .I am using firefox 28 and selenium 2.41
for selenium 4 and python one can follow this stackoverflow.com/a/71408470/3820418
4

On October 15, 2023, the code from the selected answer does not work, here is the working correctly code block:

import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.page_load_strategy = 'none'
driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 20)
driver.get('http://stackoverflow.com/')

wait.until(EC.presence_of_element_located((By.ID, 'content')))
print(driver.find_element(By.ID, 'content').get_attribute('innerHTML'))
driver.execute_script("window.stop(); alert('Content is located, loading stopped!')")
time.sleep(100)

Comments

Your Answer

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.