使用 Selenium Python API 绑定获取来自 Chrome 的 console.log 输出。


我们可以使用 Selenium Python API 绑定获取来自 Chrome 的 console.log 输出。我们将使用 DesiredCapabilities 类执行此操作。我们将使用 DesiredCapabilities.Chrome 设置从浏览器中启用日志记录。

我们必须将此浏览器功能传递给驱动程序对象,方法是将其作为参数传递给 Chrome 类。若要启用日志记录,我们将把浏览器的 goog:loggingPrefs 属性设置为 'browser':'ALL'

语法

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

示例

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import
DesiredCapabilities
#set browser log
dc = DesiredCapabilities.CHROME
dc['goog:loggingPrefs'] = { 'browser':'ALL' }
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe",
desired_capabilities=dc)
#launch browser
driver.get ("https://tutorialspoint.com/index.htm")
#obtain with get_log()
for e in driver.get_log('browser'):
   print(e)
driver.quit()

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

输出

更新于: 2021-01-30

5 千 + 次观看

开启您的职业生涯

通过完成本课程获得认证

开始
广告