0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Python + Selenium ヘッドレスブラウザ] webdriverでjsを呼び出した場合のログ確認

Posted at

ヘッドレスブラウザ上でjavascriptで処理を実行した場合どうデバッグする方法
console.log()の内容をpython側に持ってきて標準出力で確認できるようにしました。

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

# Chrome WebDriverの設定
chrome_options = Options()
chrome_options.add_argument("--headless")  # ヘッドレスモード
service = Service('/path/to/chromedriver')  # Chromedriver のパス

# WebDriverの初期化
driver = webdriver.Chrome(service=service, options=chrome_options)

# 任意のURLを開く
driver.get("https://example.com")  # ここに実際のURLを指定

result = driver.execute_script('example.js')  # ここに実際のjsファイルのパスを指定

# コンソールログを取得
logs = driver.get_log('browser')

# 結果の表示
print("JavaScript result:", result)
for log in logs:
    print("Log:", log['message'])

# ブラウザを閉じる
driver.quit()

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?