自学内容网 自学内容网

Python使用seleniumwire接管Chrome查看控制台中参数

1、cmd打开控制台,启动谷歌并制定端口号,找不到文件的加环境变量

chrome.exe --remote-debugging-port=9222

2、获取F12控制台中接口参数

from selenium.webdriver.chrome.service import Service
from seleniumwire import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_options.set_capability("browserName", 'chrome')
chrome_options.set_capability("goog:chromeOptions", {'perfLoggingPrefs': {'enableNetwork': True}, 'w3c': False})
chrome_options.set_capability("goog:loggingPrefs", {"performance": "ALL"})
service = Service(executable_path='D:\crack-plugin\chromedriver-win64\chromedriver.exe')
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.get("https://example.com")
print("已监听到网页,名称为:" + driver.title)
performance_log = driver.get_log('performance')
authorization = None
print(authorization)

3、如果需要获取针对性的参数,比如header中的登录令牌Bearer Token的话,进行针对性的写法即可

for log in performance_log:
        if "Authorization" in log['message']:
            message = json.loads(log['message'])
            if "Network.requestWillBeSentExtraInfo" == message['message']['method']:
                bearer = message['message']['params']['headers']['Authorization']
                authorization = bearer
                print("获取到登录令牌:" + bearer)


原文地址:https://blog.csdn.net/qq_57600840/article/details/145200183

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!