Monitoring JSON wire protocol logs

匿名 (未验证) 提交于 2019-12-03 01:57:01

问题:

According to the selenium documentation, interactions between the webdriver client and a browser is done via JSON Wire Protocol. Basically the client, written in python, ruby, java whatever, sends JSON messages to the web browser and the web browser responds with JSON too.

Is there a way to view/catch/log these JSON messages while running a selenium test?

For example (in Python):

from selenium import webdriver  driver = webdriver.Chrome() driver.get('http://google.com')  driver.close() 

I want to see what JSON messages are going between the python selenium webdriver client and a browser when I instantiate the driver (in this case Chrome): webdriver.Chrome(), when I'm getting a page: driver.get('http://google.com') and when I'm closing it: driver.close().

FYI, in the #SFSE: Stripping Down Remote WebDriver tutorial, it is done via capturing the network traffic between the local machine where the script is running and the remote selenium server.

I'm tagging the question as Python specific, but really would be happy with any pointers.

回答1:

When you use Chrome you can direct the chromedriver instance that will drive Chrome to log more information than what is available through the logging package. This information includes the commands sent to the browser and the responses it gets. Here's an example:

from selenium import webdriver  driver = webdriver.Chrome(service_log_path="/tmp/log") driver.get("http://www.google.com") driver.find_element_by_css_selector("input") driver.quit() 

The code above will output the log to /tmp/log. The part of the log that corresponds to the find_element_... call looks like this:

[2.389][INFO]: COMMAND FindElement {    "sessionId": "b6707ee92a3261e1dc33a53514490663",    "using": "css selector",    "value": "input" } [2.389][INFO]: Waiting for pending navigations... [2.389][INFO]: Done waiting for pending navigations [2.398][INFO]: Waiting for pending navigations... [2.398][INFO]: Done waiting for pending navigations [2.398][INFO]: RESPONSE FindElement {    "ELEMENT": "0.3367185448296368-1" } 

As far as I know, the commands and responses faithfully represent what is going on between the client and the server. I've submitted bug reports and fixes to the Selenium project on the basis of what I saw in these logs.



回答2:

Found one option that almost fits my needs.

Just piping the logger to the stdout allows to see underlying requests being made:

import logging import sys  from selenium import webdriver   # pipe logs to stdout logger = logging.getLogger() logger.addHandler(logging.StreamHandler(sys.stdout)) logger.setLevel(logging.NOTSET)  # selenium specific code driver = webdriver.Chrome() driver.get('http://google.com')  driver.close() 

It prints:

POST http://127.0.0.1:56668/session {"desiredCapabilities": {"platform": "ANY", "browserName": "chrome", "version": "", "javascriptEnabled": true, "chromeOptions": {"args": [], "extensions": []}}} Finished Request POST http://127.0.0.1:56668/session/5b6875595143b0b9993ed4f66f1f19fc/url {"url": "http://google.com", "sessionId": "5b6875595143b0b9993ed4f66f1f19fc"} Finished Request DELETE http://127.0.0.1:56668/session/5b6875595143b0b9993ed4f66f1f19fc/window {"sessionId": "5b6875595143b0b9993ed4f66f1f19fc"} Finished Request 

I don't see the responses, but this is already a progress.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!