Selenium python: how to stop page loading when loading a specific element?

Implicit and explicit waits can be used when the page is using AJAX, but I want to stop the load called by driver.get () when enough elements are loaded. Is it possible to do this because the driver.get () call only returns when the page has finished loading.

+3


source to share


1 answer


Yes, this is possible by setting the parameter to a pageLoadStrategy

value none

. Then wait for the element to be present and call window.stop

to stop 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();")

      

+9


source







All Articles