当页面使用AJAX时可以使用隐式和显式等待,但是我想停止由于加载driver.get()足够的元素而导致的加载。因为driver.get()调用仅在页面加载完成后才返回,所以可以这样做吗?
driver.get()
是的,可以通过将pageLoadStrategy功能设置为来实现none。然后等待一个元素出现并调用window.stop以停止加载:
pageLoadStrategy
none
window.stop
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();")