gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:56:08 25 4
gpt4 key购买 nike

当页面使用AJAX时可以使用隐式和显式等待,但我想在加载足够的元素时停止由driver.get()引起的加载。是否可以这样做,因为 driver.get() 调用仅在页面完成加载时返回。

最佳答案

是的,可以通过将 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();")

关于python - Selenium + Python : How to stop page loading when certain element gets loaded?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44503576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com