利用selenium获取api接口数据
Posted supramolecular
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用selenium获取api接口数据相关的知识,希望对你有一定的参考价值。
#-*-coding:utf-8-*-
import json
from selenium import webdriver
import time
import traceback
if __name__ == "__main__":
caps =
'browserName': 'chrome',
'loggingPrefs':
'browser': 'ALL',
'driver': 'ALL',
'performance': 'ALL',
,
'goog:chromeOptions':
'perfLoggingPrefs':
'enableNetwork': True,
,
'w3c': False,
,
driver = webdriver.Chrome(executable_path="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe", desired_capabilities=caps)
driver.maximize_window()
driver.get("https://xxx.com/")
driver.refresh()
time.sleep(5)
log = driver.get_log('performance')
for i in range(len(log)):
message = json.loads(log[i]['message'])
params = message['message']['params']
request = message.get('request')
print(request)
if (request is None):
continue
url = request.get('url')
print(url)
print(url is not None, isinstance(url, str) )
if url is not None and isinstance(url, str) and (url.startswith("https://xxx.com")):
print(message['requestId'])
try:
content = driver.execute_cdp_cmd('Network.getResponseBody', 'requestId': message['requestId'])
print(content)
except:
traceback.print_exc()
break
以上是关于利用selenium获取api接口数据的主要内容,如果未能解决你的问题,请参考以下文章
利用python+seleniumUI自动化登录获取cookie后再去测试接口,今天终于搞定了