本文介绍了Selenium + Python:如何在加载某些元素时停止页面加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当页面使用AJAX时可以使用隐式和显式等待,但我想在加载足够的元素时停止由driver.get()
引起的加载.是否可以这样做,因为 driver.get()
调用仅在页面完成加载时返回.
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.
推荐答案
是的,可以通过将 pageLoadStrategy
功能设置为 none
来实现.然后等待元素出现并调用 window.stop
停止加载:
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();")
这篇关于Selenium + Python:如何在加载某些元素时停止页面加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!