简体   繁体   中英

Is there any way to log http requests/responses using Selenium Webdriver (firefox)?

Is there any way to log http requests/responses using Selenium Webdriver (firefox)?

I guess it's possible to drive web traffic through proxy and log it, but maybe there is more simple "internal" selenium solution?

Asked this question on #selenium channel:

you will need to proxy it to capture the requests

so, looks like only way to setup proxy for it.

No, WebDriver doesn't have any methods to examine or modify the HTTP traffic occurring between the browser and the website. The information you've already gotten from the Selenium IRC channel (likely even from a Selenium committer) is correct. A proxy is the correct approach here.

Now year 2021, answer is: YES .

you can use the new lib: selenium-wire

How to use selenium-wire

install

pip install selenium-wire

change code

change from

from selenium import webdriver

to

from seleniumwire import webdriver

add your code

after driver.get(yourUrl) , add:

# Access requests via the `requests` attribute
for request in driver.requests:
    if request.response:
        print(
            request.url,
            request.response.status_code,
            request.response.headers['Content-Type']
        )

can got your expected request and response .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM