【问题标题】:How to get browser console error messages using Selenium WebDriver + python如何使用 Selenium WebDriver + python 获取浏览器控制台错误消息
【发布时间】:2018-12-19 19:29:23
【问题描述】:

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

【问题讨论】:

  • 不确定你想看到什么,但你可以导入 selenium 的异常模块:from selenium.common.exceptions import *。然后你可以编写 try/except 块来捕获和报告特定的 Selenium 异常。
  • 控制台错误是什么意思?
  • @RonNorris 无论我们在控制台中得到什么,我们都必须打印错误。
  • @Qwerty javascript 控制台错误或消息
  • 你是不是想通过你的程序读取Browser Console Logs

标签: python selenium logging browser selenium-chromedriver


【解决方案1】:

这个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'}]

我相信输出的就是你要找的。​​p>

https://stackoverflow.com/ 上的控制台示例:

这是我来自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}

【讨论】:

    猜你喜欢
    • 2014-08-26
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    相关资源
    最近更新 更多