LoginSignup
1
2

More than 1 year has passed since last update.

Windows10 + Python3 + Selenium4でChromeとEdgeをheadlessで起動してみる

Last updated at Posted at 2022-06-26

目的

Selenium4を使用してChromeとEdgeをheadlessで起動してみる
以下の Selenium3での内容の修正点が多すぎなのでメモを残しておく
Windows 10 + Python3 + selenium + msedgedriver で headless edge(Chromium) を試してみる
Windows 10 + Python3 + selenium + chromedriver で headless chrome を試してみる
※BeautifulSoupは整形されたhtmlの出力用に使用している

モジュールのインストール

pip install -U selenium
pip install webdriver_manager 

サンプルコード

PS > python -V
Python 3.10.5

Chrome版

# Windows Add env PYTHONIOENCODING = UTF-8 & restart vscode
# coding:utf-8

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup

options = Options()
options.add_argument('--headless')

# ダウンロードしたWebDriverのパスを直接記述する場合
# svc = Service('C:\\Dev\\tool\\webdriver\\chromedriver.exe')

svc = Service(ChromeDriverManager().install())
# [WDM] - ====== WebDriver manager ======
# [WDM] - Current google-chrome version is 103.0.5060
# [WDM] - Get LATEST chromedriver version for 103.0.5060 google-chrome
# 初めて起動する場合やバージョンが更新された場合DLする
# [WDM] - There is no [win32] chromedriver for browser 103.0.5060 in cache
# [WDM] - About to download new driver from https://chromedriver.storage.googleapis.com/103.0.5060.53/chromedriver_win32.zip
# 既にDL済の場合
# [WDM] - Get LATEST chromedriver version for 103.0.5060 google-chrome
# [WDM] - Driver [C:\Users\username\.wdm\drivers\chromedriver\win32\103.0.5060.53\chromedriver.exe] found in cache

driver = webdriver.Chrome(service=svc, options=options)

# yahoo newsにアクセス
driver.get('https://news.yahoo.co.jp/')

# htmlを取得・表示
html = driver.page_source

# 整形されたソースを表示するため
soup = BeautifulSoup(html, 'html.parser')
print(soup.prettify())

# print(html)

# ブラウザーを終了
driver.quit()

Edge版

# Windows Add env PYTHONIOENCODING = UTF-8 & restart vscode
# coding:utf-8

from bs4 import BeautifulSoup
from selenium import webdriver
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options

options = Options()
options.add_argument('headless')

# ダウンロードしたWebDriverのパスを直接記述する場合
# svc = Service('C:\\Dev\\tool\\webdriver\\msedgedriver.exe')

svc = Service(EdgeChromiumDriverManager().install())
# [WDM] - ====== WebDriver manager ======
# [WDM] - Current edge version is 102.0.1245
# 初めて起動する場合やバージョンが更新された場合DLする
# [WDM] - Get LATEST edgedriver version for 102.0.1245 Edge
# [WDM] - About to download new driver from https://msedgedriver.azureedge.net/102.0.1245.50/edgedriver_win64.zip
# 既にDL済の場合
# [WDM] - Get LATEST edgedriver version for 102.0.1245 Edge
# [WDM] - Driver [C:\Users\username\.wdm\drivers\edgedriver\win64\102.0.1245.50\msedgedriver.exe] found in cache

driver = webdriver.Edge(service=svc, options=options)

# yahoo newsにアクセス
driver.get('https://news.yahoo.co.jp/')

# htmlを取得・表示
html = driver.page_source

# 整形されたソースを表示するため
soup = BeautifulSoup(html, 'html.parser')
print(soup.prettify())

# print(html)

# ブラウザーを終了
driver.quit()

参考にしたのは以下のサイト

Selenium Webdriverの指定方法(executable_path)が非推奨になったことへの対処法
【Python】Selenium 4で、Selenium 3の書き方で書いたらWarningが出た
[Python] Seleniumで各ブラウザのDriverを自動取得
Windows10 + Python3 + BeautifulSoup4 を試してみる
Install browser drivers

1
2
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
1
2