Getting console.log output from Chrome with Seleni

2019-01-03 15:40发布

I'm using Selenium to run tests in Chrome via the Python API bindings, and I'm having trouble figuring out how to configure Chrome to make the console.log output from the loaded test available. I see that there are get_log() and log_types() methods on the WebDriver object, and I've seen Get chrome's console log which shows how to do things in Java. But I don't see an equivalent of Java's LoggingPreferences type in the Python API. Is there some way to accomplish what I need?

1条回答
地球回转人心会变
2楼-- · 2019-01-03 16:14

Ok, finally figured it out:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities    
# enable browser logging
d = DesiredCapabilities.CHROME
d['loggingPrefs'] = { 'browser':'ALL' }
driver = webdriver.Chrome(desired_capabilities=d)
# load some site
driver.get('http://foo.com')
# print messages
for entry in driver.get_log('browser'):
    print entry

Entries whose source field equals 'console-api' correspond to console messages, and the message itself is stored in the message field.

查看更多
登录 后发表回答