前提・実現したいこと
ブラウザを表示しての自動処理を実現したく、PythonとSeleniumを使って、Chromeを操作するコードを作成しました。
powershell
1# 必要モジュールのインストール 2> pip install selenium 3> pip install chromedriver_binary
python
1from selenium import webdriver 2import chromedriver_binary 3driver = webdriver.Chrome() 4driver.get("http://kakaku.com/") 5print(driver.title) 6print(driver.current_url) 7driver.find_element_by_id("query").send_keys("スマートフォン")
ここまでできましたので、ChromeOptionsで既存のユーザープロファイルを指定して、Chromeを起動したい と思っています。
このため、以下のようにChromeOptionsを追加しました。
python
1from selenium import webdriver 2import chromedriver_binary 3options = webdriver.ChromeOptions() 4options.add_argument(r'--user-data-dir=D:\Users[User Name]\AppData\Local\Google\Chrome\User Data') 5options.add_argument(r'--profile-directory=Default') 6driver = webdriver.Chrome(options=options) 7# 以降同じ
発生している問題・エラーメッセージ
Chromeの表示直後に次のようなエラーが発生し、以降の処理の実行に失敗します。
既存のブラウザ セッションで開いています。 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python37\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__ desired_capabilities=desired_capabilities) File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__ self.start_session(capabilities, browser_profile) File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location C:\Program Files (x86)\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.) (Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 10.0.17763 x86_64)
一度taskkill /im chrome.exe /F
で残存しているChromeを全て強制終了してからプログラムを実行すると、正常に動作しますが、「[7036:23384:0114/230205.683:ERROR:in_progress_cache_impl.cc(203)] Cache is not initialized, cannot RetrieveEntry.」というようなログが大量に表示されます。
解決したいこと
プログラム実行時に「Chromeが他に起動していないことを確認する」という手順は入れたくありませんので、できれば既存のChromeが起動している状態でもエラーが発生しないようにしたいのですが、何か良い方法はないでしょうか?
また、それが仕様上無理な場合、できれば無理な理由を自分でも説明できるようにしたいと思っています。何か理由がわかる情報源はないでしょうか。
他に試してみたこと
--user-data-dir
を起動中のChromeと別の値に設定する
- Chromeが既に起動している状態
--user-data-dir
を既に起動しているChromeと別のフォルダに指定した場合
エラーが発生しませんでした。
python
1from selenium import webdriver 2import chromedriver_binary 3options = webdriver.ChromeOptions() 4options.add_argument(r'--user-data-dir=Data') 5options.add_argument(r'--profile-directory=Default') 6# 以降同じ
ここで起動したブラウザにGoogleアカウントを設定した場合、その情報も保存されていることを確認しました。
--profile-directory
を起動中のChromeと別の値に設定する
- Chromeが既に起動している状態
--user-data-dir
を既に起動しているChromeと同じフォルダ--profile-directory
を現在起動しているChromeとは別の値に指定した場合
上記と同じWebDriverExceptionが発生しました。
python
1from selenium import webdriver 2import chromedriver_binary 3options = webdriver.ChromeOptions() 4options.add_argument(r'--user-data-dir=D:\Users[User Name]\AppData\Local\Google\Chrome\User Data') 5options.add_argument(r'--profile-directory=Profile 1') 6# 以降同じ
補足情報(FW/ツールのバージョンなど)
- selenium:3.141.0(C:\Python37\Lib\site-packages\selenium_init_.pyに記載のもの)
- chromedriver:2.45.615291
- Windows 10:1809 17763.253
- Chrome:バージョン: 71.0.3578.98(Official Build) (64 ビット)

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/15 13:23
2025/02/18 22:00