美文网首页python随记
selenium chrome cdp network data

selenium chrome cdp network data

作者: LCSan | 来源:发表于2020-05-05 12:03 被阅读0次
#coding=utf-8
'''
Created on 2020年5月5日

@author: 瞌睡蟲子
'''
from splinter import Browser
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import json

chrome_options = webdriver.ChromeOptions()
# 至关重要,否则get_log("performance")会报performance not found
chrome_options.add_experimental_option('w3c', False)
caps = DesiredCapabilities.CHROME
caps['loggingPrefs'] = {'performance': 'ALL'}
  
with Browser(driver_name='chrome', executable_path='D:/chromedriver.exe',desired_capabilities=caps,options=chrome_options) as b:
    b.visit('https://www.baidu.com/')
    screenshot_path = b.find_by_xpath('//*[@id="s_lg_img"]').first.screenshot(r'C:\Users\Administrator\Desktop\your_screenshot.png')
    print(screenshot_path)  
    driver = b.driver
    # 通过performance获取network的requestId
    browser_log_list = driver.get_log("performance")
    logs = [json.loads(log['message'])['message'] for log in browser_log_list]
    res=[]
    for i in logs:
        if i["method"] == "Network.requestWillBeSent":
            requestId = i["params"]["requestId"]
            # 通过cdp获取response body
            response_dict = driver.execute_cdp_cmd('Network.getResponseBody', {'requestId': requestId})
            res.append(i["params"]["request"]["url"])
            res.append(response_dict)
    with open(r'C:\Users\Administrator\Desktop\devtools.json', 'w') as f:
        json.dump(res, f)

相关文章

网友评论

    本文标题:selenium chrome cdp network data

    本文链接:https://www.haomeiwen.com/subject/cbqmghtx.html