Selenium経由でChromeの開発者ツールのrequest URLを取得する

やりたいこと

Selenium経由でChromeの開発者ツールでみえるようなrequest URLを取得する

原因

質問でチラ見した

やり方

  • driver.get_log でログをゲットしてrequest でフィルタする

参考にしたサイト(というかほぼそのまま)

blog.motikan2010.com

別の要素でフィルタすることもできそう

github.com

  • 実際のやり方
# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
import json

url = 'https://www.google.co.jp/'

options = webdriver.ChromeOptions()
options.set_capability("goog:loggingPrefs", {"performance": "ALL"})
driver = webdriver.Chrome(options=options,)
driver.get(url)

for entry_json in driver.get_log('performance'):
    entry = json.loads(entry_json['message'])
    if entry['message']['method'] != 'Network.requestWillBeSent' :
        continue
    print(entry['message']['params']['request']['url'])

driver.close()