簡體   English   中英

"使用 selenium 在 Chrome 控制台上捕獲 console.log 輸出"

[英]Capture console.log outputs on Chrome console using selenium

我有一個網站列表,我正在對其進行一些測試和實驗。 我使用 selenium 訪問列表中的一個網站,並使用 MITMproxy 腳本將一段 JS 注入到一些文件中。 這個注入的代碼執行一些測試,並在 JS 中使用 console.log() 將結果輸出到 chrome 控制台,類似於

console.log(results of the injected JS)

注入成功,當我運行實驗時,我希望的結果確實出現在 Chrome 控制台上。 我面臨的問題是當我嘗試捕獲 chrome 控制台以進行 console.log 輸出時,它沒有成功。 它將從 chrome 控制台捕獲警告和錯誤消息,但不會捕獲console.log輸出。 目前這是我的做法。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException, TimeoutException, StaleElementReferenceException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

option = Options()
option.add_argument('start-maximized')

# Specify the proxy
option.add_argument('--proxy-server=%s:%s' % (proxy_host, proxy_port))

# enable browser logging
d = DesiredCapabilities.CHROME
# d['loggingPrefs'] = { 'browser':'ALL' }
d['goog:loggingPrefs'] = { 'browser':'ALL' }


# Launch Chrome.
driver = webdriver.Chrome(options=option, executable_path = './chromedriver', desired_capabilities=d, service_args=["--verbose", "--log-path=./js_inject/qc1.log"])

for url in list_urls:
# Navigate to the test page

    driver.get(url)
    sleep(15)
    # in this 15 seconds, the MITMproxy will inject the code and the injected code will output on chrome console.
    for entry in driver.get_log('browser'):
        print(entry)

誰能指出我可能犯的錯誤或執行此任務的替代方法。 謝謝你。

PS 請原諒我的語法錯誤。

options.add_experimental_option('excludeSwitches', ['enable-logging'])
dc = DesiredCapabilities.CHROME
dc["goog:loggingPrefs"] = {"browser":"INFO"}
self.driver = webdriver.Chrome(chrome_options=options, desired_capabilities=dc)

不久前我設法讓它工作,所以不確定它到底做了什么,但我認為這是“anable-logging”的實驗選項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM