Python Browser Console Log Objects

Viewed 34

I'm currently using selenium to get console logs from a chrome browser. My setup code is this and it works well for getting console logs

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

# enable browser logging
d = DesiredCapabilities.CHROME
d['goog:loggingPrefs'] = { 'browser':'ALL' }
driver = webdriver.Chrome(desired_capabilities=d)

# load the desired webpage
driver.get(http://192.168.5.5)

# print messages
for entry in driver.get_log('browser'):
    print(entry)

The problem is that entry is a dictionary that contains a string of Objects. I can not access what is inside this Object. In a real browser I can expand on these objects and see further information which is usually in the form of a dictionary. How do I get access to these console log object's in python like I would on a browser? It does not have to be through selenium if it is not possible in selenium.

Edit:

Example of the output from entry:

{'level': 'INFO', 'message': 'http://192.168.5.5/app.e52b3dcc.bundle.js 58:31633 "[END REQUEST]: Response:" Object Object', 'source': 'console-api', 'timestamp': 1663609942539}

Example of the object expanded in the browser (The copy and paste doesn't look as good here but you get the idea):

```END REQUEST]: Response: 
Object
data1
: 
"SOMEPIECEOFDATA"
response
: 
{moreData: {…}, status: 1, type: 'response'}
[[Prototype]]
: 
Object
1 Answers

Your code is essentially valid. Take this page with your question for example:

caps['goog:loggingPrefs'] = {'browser':'ALL' }
[...]
url = 'https://stackoverflow.com/questions/73778005/python-browser-console-log-objects'
driver.get(url)
logs = driver.get_log('browser')
for item in logs:
    print(item)

Result in terminal:

{'level': 'WARNING', 'message': "security - Error with Feature-Policy header: Unrecognized feature: 'speaker'.", 'source': 'security', 'timestamp': 1663614945300}
{'level': 'WARNING', 'message': 'https://pagead2.googlesyndication.com/gpt/pubads_impl_2022091301.js 9:37758 "The following functions are deprecated: googletag.pubads().setTagForChildDirectedTreatment(), googletag.pubads().clearTagForChildDirectedTreatment(), googletag.pubads().setRequestNonPersonalizedAds(), and googletag.pubads().setTagForUnderAgeOfConsent(). Please use googletag.pubads().setPrivacySettings() instead."', 'source': 'console-api', 'timestamp': 1663614947286}

Maybe you need to investigate the logging process itself?

Related