Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
685 views
in Technique[技术] by (71.8m points)

python - Parse Chrome Devtools Protocol Network JSON Response

Using python/selenium and Chrome Devtools Protocol, I am going to my webpage and looking at all the network events that happen in chrome devtools. There are quite a few events that fire when the page loads. I want to assert that the JSON results contain a certain key:value pair and POST method.

Currently with the script I have, it gives me back a few JSON responses. My Python skills are somewhat lacking as I am coming from Ruby. All I really need to do is assert that the response contains something. Maybe I need to convert the response to a string and then do an assertion with selenium, I'm not sure.

Most of the code below came from a tutorial. I know the output function is what is printing the results to my terminal. The tab.set_listener("Network.requestWillBeSent", output) is coming from Chrome Devtools Protocol. Any help at all would be great on how to assert using selenium that the response body contains a certain string would be great.

This probably isn't too tough, but I just can't seem to figure it out. Many thanks in advance.

import pychrome
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager


def output(**kwargs):
    print(kwargs)


options = webdriver.ChromeOptions()
options.add_argument("--remote-debugging-port=8000")
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=options)

dev_tools = pychrome.Browser(url="http://localhost:8000")
tab = dev_tools.list_tab()[0]
tab.start()

tab.call_method("Network.enable", _timeout=20)
tab.set_listener("Network.requestWillBeSent", output)

driver.get("https://mywebsite.com")

driver.quit()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...