LoginSignup
0
2

More than 1 year has passed since last update.

SeleniumとEdgeDriverでデフォルトのダウンロードフォルダを変更する(msedge-selenium-tools)

Last updated at Posted at 2022-03-28

追記

msedge-selenium-toolsは非推奨らしいので、selenium4で対応する方法をかきました
https://qiita.com/pm00/items/1eaf7d76ad68a4dcdf4d

はじめに

Pythonとseleniumを使ってMicrosoft Edgeのダウンロードフォルダを指定する。

準備

Edgedriverをダウンロード

PCにインストールされてるEdgeに対応するバージョンのEdgedriverをダウンロード
hしておく
https://developer.microsoft.com/ja-jp/microsoft-edge/tools/webdriver/

ライブラリのインポート

pip install selenium
pip install msedge-selenium-tools

※ただし、msedge-selenium-toolsは非推奨?になっているっぽい。selenium4に統合されたのでそっちを使えとのこと。
※以下はMicrosoft公式のマニュアル。日本語翻訳は機械翻訳がアレなので英語版で読むのを推奨
https://docs.microsoft.com/en-us/microsoft-edge/webdriver-chromium/?tabs=python
https://docs.microsoft.com/en-us/microsoft-edge/webdriver-chromium/capabilities-edge-options

実装

#%%
from msedge.selenium_tools import Edge, EdgeOptions
options = EdgeOptions()
options.use_chromium = True
options.add_experimental_option("prefs", {
  "download.default_directory": path_downloads
})
driver = Edge(executable_path=path_edgedriver, options=options)

おまけ

subprocessでEdgeのバージョンを取得する。
powershellに渡すコマンドの中に、"も、'も、どちらも使われているので、"""で囲むことにした。

#%%
t="""powershell -Command "(get-item 'c:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe').VersionInfo.FileVersion"""
import subprocess
s=subprocess.run(t,shell=True,capture_output=True)
print(str(s.stdout).split(".")[0][2:])
0
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
0
2