如何使用Selenium WebDriver + python获取浏览器控制台错误消息

时间:2018-01-10 13:53:13

标签: python selenium logging browser selenium-chromedriver

Console error png 我们一直在尝试使用以下代码打印特定网站的控制台错误。但是我们无法捕获控制台错误。任何人都可以快速回应 但我们正在接受

代码行:

driver.get_log('browser')

错误:

[{u'source': u'deprecation', u'message': u"https://xxxxx/static/vendor/vendor.bundle.js?v=16 902 Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.", u'timestamp': 1515593047810, u'level': u'WARNING'}]

代码行:

driver.get_log('driver')

错误:

[{u'timestamp': 1515593061561, u'message': u'Unable to evaluate script: disconnected: not connected to DevTools\n', u'level': u'WARNING'}, {u'timestamp': 1515593071847, u'message': u'Unable to evaluate script: disconnected: not connected to DevTools\n', u'level': u'WARNING'}]

代码块:

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

capabilities = DesiredCapabilities.CHROME
capabilities['loggingPrefs'] = { 'browser':'ALL' }
driver = webdriver.Chrome(desired_capabilities=capabilities)
driver.get('url')
elem = driver.find_element_by_id('username')
elem.send_keys('xxxx')
elem1 = driver.find_element_by_name('password')
elem1.send_keys('xxxx')
elem2 = driver.find_element_by_class_name('btn-info')
elem2.click()
driver.get('url')
for entry in driver.get_log('browser'):
    print entry

1 个答案:

答案 0 :(得分:2)

这个Error:看起来像是在打印JS错误:

[{u'source': u'deprecation', u'message': 
 u"https://xxxxx/static/vendor/vendor.bundle.js?v=16 902 Synchronous 
 XMLHttpRequest on the main thread is deprecated because of its detrimental 
 effects to the end user's experience. For more help, check 
 https://xhr.spec.whatwg.org/.", u'timestamp': 1515593047810, u'level': 
 u'WARNING'}]

我相信输出的是你正在寻找的东西。

来自https://stackoverflow.com/的控制台的示例: enter image description here

这是我for log in driver.get_log('browser'): print(log)的输出:

{'level': 'SEVERE', 'message': 'https://secure.quantserve.com/quant.js - 
Failed to load resource: net::ERR_CONNECTION_RESET', 'source': 'network', 
'timestamp': 1515630280361}
{'level': 'SEVERE', 'message': 'https://js-sec.indexww.com/ht/p/185901-
159836282584097.js - Failed to load resource: net::ERR_TIMED_OUT', 'source': 
'network', 'timestamp': 1515630288646}
{'level': 'SEVERE', 'message': 
'https://www.googletagservices.com/tag/js/gpt.js - Failed to load resource: 
net::ERR_TIMED_OUT', 'source': 'network', 'timestamp': 1515630288785}