python selenium 抓取控制台console的js log信息

import codecs
import json

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

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

driver.get('http://foo.com')

# print console log messages
info=[]
for entry in driver.get_log('browser'):
if entry['level']=='SEVERE':
info.append(entry)
j = json.dumps(info, indent=4,ensure_ascii=False)
with codecs.open('info.json', "w", "utf-8") as f:
f.write(j)


driver.close()
posted @ 2020-04-03 10:21  fyangq  阅读(6001)  评论(1编辑  收藏  举报