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


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.

Asked By: RamKumar



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 import WebDriverWait
from import expected_conditions as EC
from import By

capa = DesiredCapabilities.CHROME
capa["pageLoadStrategy"] = "none"

driver = webdriver.Chrome(desired_capabilities=capa)
wait = WebDriverWait(driver, 20)


wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#h-top-questions')))

Answered By: Florent B.
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.