質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Internet Explorer

Internet Explorer(IE;MSIE)はマイクロソフトが開発したウェブブラウザです。Microsoft Windowsに組み込まれています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

Q&A

解決済

2回答

7040閲覧

PythonのSeleniumでIEを操作したいが、webdriver_managerでValueErrorが発生

yutchan

総合スコア5

Internet Explorer

Internet Explorer(IE;MSIE)はマイクロソフトが開発したウェブブラウザです。Microsoft Windowsに組み込まれています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

0グッド

0クリップ

投稿2021/08/10 01:45

編集2021/08/11 06:42

前提・実現したいこと

Python(3.7)のSeleniumでIEでしか動作しない社内システムのスクレイピングに取り組んでいます。
そこで、webdriver_managerのIEDriverManagerを使いたいと考えております。

発生している問題

以下のように記述したのですが、ValueErrorが発生します。
エラー無くドライバをダウンロードさせるには、どのように記述すれば良いのでしょうか?

因みに、こちらのブログを参考にさせて頂いています。
https://yuki.world/python-selenium-chromedriver-auto-update/#t_Internet_ExplorerIEDriver

何卒、アドバイスをお願い致します。

発生している問題・エラーメッセージ

C:\work>python 01_ie_test.py [WDM] - There is no [x64] IEDriverServer for browser in cache Traceback (most recent call last): File "01_ie_test.py", line 4, in <module> driver_path = IEDriverManager().install() File "C:\Python\lib\site-packages\webdriver_manager\microsoft.py", line 26, in install return self._get_driver_path(self.driver) File "C:\Python\lib\site-packages\webdriver_manager\manager.py", line 29, in _get_driver_path file = download_file(driver.get_url()) File "C:\Python\lib\site-packages\webdriver_manager\driver.py", line 168, in get_url major, minor, patch = self.__get_divided_version(self.get_version()) File "C:\Python\lib\site-packages\webdriver_manager\driver.py", line 183, in __get_divided_version "but given was: {version}".format(version=self.get_version())) ValueError: Version must consist of major, minor and/or patch, but given was: .3.150.2

試したコード

python

1from selenium import webdriver 2from webdriver_manager.microsoft import IEDriverManager 3 4driver_path = IEDriverManager().install() 5driver = webdriver.Ie(driver_path) 6driver.get('https://google.com')

自分で試したこと

Python\Lib\site-packages\webdriver_manager\にある
microsoft.pyのIEDriverManager()の__init__の引数urlに記載の
http://selenium-release.storage.googleapis.com
にアクセスしたところ、以下の記述があることを発見しました。

html

1<Contents> 2<Key>3.150/IEDriverServer_Win32_.3.150.2.zip</Key> 3<Generation>1628200845472844</Generation> 4<MetaGeneration>1</MetaGeneration> 5<LastModified>2021-08-05T22:00:45.502Z</LastModified> 6<ETag>"6f17f688d643c40ea21c2610b55d259f"</ETag> 7<Size>1062956</Size> 8</Contents> 9 10 11<Contents> 12<Key>3.150/IEDriverServer_x64_.3.150.2.zip</Key> 13<Generation>1628200846705412</Generation> 14<MetaGeneration>1</MetaGeneration> 15<LastModified>2021-08-05T22:00:46.734Z</LastModified> 16<ETag>"93ddb5c8a84a304b9ed915431c79d816"</ETag> 17<Size>1154724</Size> 18</Contents>

__get_divided_versionのversionをsplitしたとき、
versionの中身は「.3.150.2」となっており、
splitしたら[],[3],[150],[2]となってlen(divided_version)が4になってValueErrorになっている
ということで、
Python\Lib\site-packages\webdriver_manager\にある
driver.pyの
class IEDriver(Driver):の
__get_divided_versionの2行目に
version = version[1:]
を追加してみました。(下記コードの2行目です。)

python

1 def __get_divided_version(self, version): 2 version = version[1:] 3 divided_version = version.split('.') 4 if len(divided_version) == 2: 5 return divided_version[0], divided_version[1], '0' 6 elif len(divided_version) == 3: 7 return divided_version 8 else: 9 raise ValueError( 10 "Version must consist of major, minor and/or patch, " 11 "but given was: {version}".format(version=self.get_version()))

すると以下のように、len(divided_version) == 3になりますので、
ダウンロードを試みるところまで進んだのですが、
ピリオドを削除した影響でurlのピリオドも消え、リンクが正しくなくなり、
ダウンロードに失敗しました。

python

1C:\work>python 01_ie_test.py 2 3 4====== WebDriver manager ====== 5There is no [Win32] IEDriverServer for browser in cache 6Trying to download new driver from http://selenium-release.storage.googleapis.com/3.150/IEDriverServer_Win32_3.150.2.zip 7Traceback (most recent call last): 8 File "C:\work\01_ie_test.py", line 4, in <module> 9 driver_path = IEDriverManager(os_type = "win32").install() 10 File "C:\Python\lib\site-packages\webdriver_manager\microsoft.py", line 25, in install 11 return self._get_driver_path(self.driver) 12 File "C:\Python\lib\site-packages\webdriver_manager\manager.py", line 30, in _get_driver_path 13 file = download_file(driver.get_url()) 14 File "C:\Python\lib\site-packages\webdriver_manager\utils.py", line 93, in download_file 15 validate_response(response) 16 File "C:\Python\lib\site-packages\webdriver_manager\utils.py", line 79, in validate_response 17 raise ValueError("There is no such driver by url {}".format(resp.url)) 18ValueError: There is no such driver by url http://selenium-release.storage.googleapis.com/3.150/IEDriverServer_Win32_3.150.2.zip

ここまでした時点で、一旦質問してみようと考え、投稿致します。
最後まで見て頂いて、ありがとうございます。

又、別で質問しようかと思っているのですが、
IEはもう使用出来なくなりますので、PythonのSeleniumでEdgeのIEモードを操作する方法について
どなたかご存じありませんでしょうか。
C#だと・・・

https://qiita.com/satata/items/47e8b2422e9238f1a7ef

https://stackoverflow.com/questions/63361687/is-there-a-way-to-activate-ie-mode-in-edge-options

あたりが上位に表示されますが、pythonで同様のことをしようとすると、calabilitiesを適切に設定
すれば出来るのでしょうか・・・

https://www.seleniumqref.com/api/python/browser/Python_DesiredCapabilities_set.html

後ほど、新たに質問を立てるつもりです。
下記に質問を立てました。
https://teratail.com/questions/353567

補足情報(FW/ツールのバージョンなど)

利用環境
OS:windows10

python --version

Python 3.7.3

pip list

Package Version


altgraph 0.17
appdirs 1.4.4
astroid 2.5
beautifulsoup4 4.9.3
certifi 2019.3.9
chardet 3.0.4
colorama 0.4.4
comtypes 1.1.8
configparser 5.0.1
crayons 0.4.0
et-xmlfile 1.0.1
future 0.18.2
gitdb 4.0.5
GitPython 3.1.13
html5lib 1.0.1
idna 2.8
isort 5.7.0
jdcal 1.4
lazy-object-proxy 1.5.2
lint 1.2.1
lxml 4.3.3
mccabe 0.6.1
MouseInfo 0.1.3
msedge-selenium-tools 3.141.3
numpy 1.16.2
opencv-python 4.5.1.48
openpyxl 2.6.2
overlay 1.1.5
pandas 0.24.2
pefile 2019.4.18
Pillow 8.1.1
pings 0.0.1
pip 21.2.3
psutil 5.8.0
PyAutoGUI 0.9.52
PyGetWindow 0.0.9
pyinstaller 4.1
pyinstaller-hooks-contrib 2020.10
pylint 2.7.1
PyMsgBox 1.0.9
pyperclip 1.8.2
PyRect 0.1.4
PyScreeze 0.1.26
python-dateutil 2.8.0
PyTweening 1.0.3
pytz 2019.1
pywin32 300
pywin32-ctypes 0.2.0
requests 2.21.0
selenium 3.141.0
setuptools 40.8.0
six 1.12.0
smmap 3.0.5
soupsieve 2.2
toml 0.10.2
tqdm 4.56.1
typed-ast 1.4.2
urllib3 1.24.1
webdriver-manager 3.2.2
webencodings 0.5.1
wheel 0.35.1
wrapt 1.12.1
xlrd 1.2.0
zenhan 0.5.2

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

下記のようにdriver.pyを改造すると、.3.150.2でも自動でダウンロードすることが出来ました。

1.__get_divided_versionで邪魔なピリオドがあれば削除し、以降のコードが正しく動くようにします。
2.get_urlでは、邪魔なピリオドが必要なので、major_period_flg を追加
3.邪魔ピリオドの有無を覚えておくために__init__にてself.major_period_flgを追加
major_period_flgは、初めTrue,Falseで作り始めたのですが、get_urlにてformat関数で置換する
とき簡単だったので、文字列"."にしてしまいました。

C:\Python\Lib\site-packages\webdriver_manager\driver.py

python

1class IEDriver(Driver): 2 def __init__(self, name, version, 3 os_type, 4 url, 5 latest_release_url): 6 7 if os_type == "win64": 8 os_type = "x64" 9 else: 10 os_type = "Win32" 11 super(IEDriver, self).__init__(version=version, 12 os_type=os_type, 13 url=url, 14 latest_release_url=latest_release_url, 15 name=name) 16 self.browser_version = "" 17 self.major_period_flg = "" #追加 18 19 def get_url(self): 20 major, minor, patch = self.__get_divided_version(self.get_version()) 21 22 if self.major_period_flg == true: 23 return ("{url}/{major}.{minor}/" 24 "{name}_{os}_{major_period_flg}{major}.{minor}.{patch}.zip").format( 25 url=self._url, name=self.get_name(), os=self.get_os_type(), 26 major_period_flg = self.major_period_flg, 27 major=major, minor=minor, patch=patch) #変更 major_period_flg追加 28 29 def __get_divided_version(self, version): 30 if version[0] == ".": #追加 もし要らないピリオドがついていたら 31 version = version[1:] #追加 初めのピリオドを削除 32 self.major_period_flg = "." #追加 フラグにピリオドを追加 33 34 divided_version = version.split('.') 35 if len(divided_version) == 2: 36 return divided_version[0], divided_version[1], '0' 37 elif len(divided_version) == 3: 38 return divided_version 39 else: 40 raise ValueError( 41 "Version must consist of major, minor and/or patch, " 42 "but given was: {version}".format(version=self.get_version())) 43

このようにdriver.pyを変更し、以下のコードによりEdgeのIEモードがpythonで起動しました。

python

1from selenium import webdriver 2from webdriver_manager.microsoft import IEDriverManager 3 4ieOptions = webdriver.IeOptions() 5ieOptions.add_additional_option("ie.edgechromium", True) 6ieOptions.add_additional_option("ie.edgepath",'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe') 7driver = webdriver.Ie(executable_path=IEDriverManager().install(), options=ieOptions) 8driver.get('https://google.com')

投稿2021/09/01 06:15

yutchan

総合スコア5

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

最新バージョンだけバージョン表記が間違っているようです。
回避するだけでしたらバージョン指定を行えば大丈夫なようです。

python

1driver_path = IEDriverManager(version='3.150.1').install()

投稿2021/08/28 08:15

TANAKAKazuyoshi

総合スコア96

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yutchan

2021/09/01 05:37

教えて頂きありがとうございます。 まさか間違っているなんて、思いもしなかったので、何か理由があるのかなーと思ってはまってしまいました。 間違っているなら、対処しようということで、余分なピリオドが付いていた場合に回避する方法を考えてみました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問