Jeopardy
Jeopardy

Reputation: 13

selenium chromedriver read all console.log parameters

I open website with selenium chrome driver and read from console. Problem is that I can only see the first parameter. For example this console.log("first") gives me "first" as expected, but console.log("first", "second") gives me only "first".

from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

options = Options()
options.add_argument('--load-extension=./extension')
options.add_argument("--disable-notifications")
options.add_argument("--start-maximized")

d = DesiredCapabilities.CHROME
d['loggingPrefs'] = {'browser': 'ALL'}
self.browser = webdriver.Chrome(chrome_options=options, desired_capabilities=d)

I expect that I should be able to read all parameters, just as I can see them logged in the console window. Is there any option for selenium chromedriver that gives me all of them?

Upvotes: 0

Views: 1514

Answers (1)

Kafels
Kafels

Reputation: 4069

Your code is correct, maybe you're not seeing it when running your code.

I'll demonstrate a example below with same configuration as you and getting all console parameters output

import re

from selenium import webdriver
from selenium.webdriver import DesiredCapabilities

dc = DesiredCapabilities.CHROME
dc['loggingPrefs'] = {'browser': 'ALL'}
driver = webdriver.Chrome(desired_capabilities=dc)

# Prints 'first'
driver.execute_script("console.log('first')")
# Prints 'first' 'second'
driver.execute_script("console.log('first', 'second')")

# Filtering log by key 'first'
logs = list(filter(lambda l: 'first' in l['message'], driver.get_log('browser')))
for log in logs:
    source = log['source']
    parameters = re.findall(r'".*?"', log['message'])

    # Prints console output and parameters
    print(source, *parameters)

driver.quit()

Output console:

console-api "first"
console-api "first" "second"

Upvotes: 1

Related Questions