Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --log-cdp / log_cdp=True options for capturing CDP logs #2220

Closed
mdmintz opened this issue Oct 29, 2023 · 6 comments · Fixed by #2221
Closed

Add --log-cdp / log_cdp=True options for capturing CDP logs #2220

mdmintz opened this issue Oct 29, 2023 · 6 comments · Fixed by #2221
Assignees
Labels
enhancement Making things better

Comments

@mdmintz
Copy link
Member

mdmintz commented Oct 29, 2023

Add --log-cdp / log_cdp=True options for capturing CDP logs

If enabled, this happens:

chrome_options.set_capability(
    "goog:loggingPrefs", {"performance": "ALL", "browser": "ALL"}
)

Then you can do something like this to display those logs: (Eg. driver.get_log("performance"))

from seleniumbase import Driver

driver = Driver(uc=True, log_cdp=True)
try:
    driver.get("https://seleniumbase.io")
    driver.sleep(3)
    print(driver.get_log("performance"))
finally:
    driver.quit()

Note that UC Mode also has separate --uc-cdp / uc_cdp=True options. That works differently. (See SeleniumBase/examples/uc_cdp_events.py for an example of that.)

@mdmintz
Copy link
Member Author

mdmintz commented Oct 29, 2023

@MovingW
Copy link

MovingW commented Oct 29, 2023

That's GREAT!!
Thanks for your quick response Michael.

@KeeonTabrizi
Copy link

@mdmintz so if i create a driver as follows

from seleniumbase import Driver

driver = Driver(uc=True, incognito=True, uc_cdp_events=True)

How can I look at a specific request and the response body for that request?

@mdmintz
Copy link
Member Author

mdmintz commented Dec 12, 2023

#2050 (comment) - To get the response body of a request, first get the requestId. Then, you'll want to execute the following: driver.execute_cdp_cmd("Network.getResponseBody", { "requestId": "<request id>" })

@KeeonTabrizi
Copy link

#2050 (comment) - To get the response body of a request, first get the requestId. Then, you'll want to execute the following: driver.execute_cdp_cmd("Network.getResponseBody", { "requestId": "<request id>" })

@mdmintz thanks so much! I'm trying to do something simple to debug...

URL = # define here

def callback(data):
    request_id = data['requestId']
    print(request_id)
    print(data)


driver.add_cdp_listener("*", callback)
driver.get(URL)

When I just use the callback to print(data) I see lots of different things captured (though I still can't find my outgoing request I'm targeting that I can see with seleniumwire. I figured I'd try to just print the requestIds but that portion doesn't seem to print.

With the command you also suggested

driver.execute_cdp_cmd("Network.getResponseBody", { "requestId": "<request id>" })

Where would I actually be able to look at the response body? When running the command would I expect something returned in my console/script? I basically want to store the response body for that particular request (asssuming I can find it) to a variable to then decode.

@mdmintz
Copy link
Member Author

mdmintz commented Dec 12, 2023

See https://stackoverflow.com/a/52636871/7058266 for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Making things better
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants