#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)
网友评论