fang

Python Selenium 加载某些元素时停止页面加载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# encoding: utf-8
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait       #WebDriverWait注意大小写
from selenium.webdriver.common.by import By
from selenium import webdriver
import re

def start(driver):

    driver.get("https://baidu.com")
    wait.until(EC.presence_of_element_located((By.XPATH, "//div[@id='article']")))  # 出现article元素时 停止加载
    driver.execute_script("window.stop(); ")
    content = driver.find_element(By.XPATH, "//div[@id='article']").get_attribute('innerHTML')
    #print(content)
   

if __name__ == '__main__':
    capa = DesiredCapabilities.CHROME
    capa["pageLoadStrategy"] = "none"
    options = webdriver.ChromeOptions()
    options.add_argument('--ignore-certificate-errors')  # 忽略掉那些证书错误
    options.add_experimental_option('excludeSwitches', [
        'enable-automation'])  # Bluetooth: bluetooth_adapter_winrt.cc:1072 Getting Default Adapter failed.

    options.add_argument(r"user-data-dir=C:\Users\Administrator\AppData\Local\Google\Chrome\User Data1")
    driver = webdriver.Chrome(executable_path="C:\Program Files\Google\Chrome\Application\chromedriver.exe",
                              options=options, chrome_options=options,
                              desired_capabilities=capa)  # 没有把Chromedriver放到python安装路径
    wait = WebDriverWait(driver, 20)

    start(driver)
赞(0) 打赏
标签:

上一篇:

下一篇:

相关推荐

0 条评论关于"Python Selenium 加载特定元素 停止页面加载"

表情

最新评论

    暂无留言哦~~

支付宝扫一扫打赏

微信扫一扫打赏