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

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

新規登録して質問してみよう
ただいま回答率
85.48%
スクレイピング

スクレイピングとは、公開されているWebサイトからページ内の情報を抽出する技術です。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

selenium

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

Q&A

1回答

4039閲覧

Python Selenium profileを指定し実行すると unknown error: DevToolsActivePort file doesn't exist が発生する

hello_water

総合スコア0

スクレイピング

スクレイピングとは、公開されているWebサイトからページ内の情報を抽出する技術です。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

selenium

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

0グッド

0クリップ

投稿2022/12/26 14:29

実現したいこと

PythonでSeleniumを利用したスクレイピングを実装しようと考えています。
スクレイピング時に保存しているユーザー名やパスワードを利用したいため、Profileを指定して起動します。

動かしてみるとGoogleChrome自体は起動しますが、下記のエラーで該当のURLへアクセスできません。
どなたかご教授いただけないでしょうか。

また、以下の内容では正常に動きました。

  • optionsを適用しない
  • Profileを指定しない

エラー抜粋

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited normally. (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location C:/Program Files/Google/Chrome/Application/chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

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

処理を開始します �����̃u���E�U �Z�b�V�����ŊJ���Ă��܂��B Traceback (most recent call last): File "d:\Docs\Python\mercariSeleniumAutomation\Setting.py", line 16, in <module> driver = webdriver.Chrome(options=options) File "C:\Users\userName\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__ super().__init__( File "C:\Users\userName\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 106, in __init__ super().__init__( File "C:\Users\userName\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 288, in __init__ self.start_session(capabilities, browser_profile) File "C:\Users\userName\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 381, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "C:\Users\userName\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 444, in execute self.error_handler.check_response(response) File "C:\Users\userName\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 249, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited normally. (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location C:/Program Files/Google/Chrome/Application/chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.) Stacktrace: Backtrace: (No symbol) [0x005BF243] (No symbol) [0x00547FD1] (No symbol) [0x0043D04D] (No symbol) [0x0045C24E] (No symbol) [0x004582E9] (No symbol) [0x0048F056] (No symbol) [0x0048EB2A] (No symbol) [0x00488386] (No symbol) [0x0046163C] (No symbol) [0x0046269D] GetHandleVerifier [0x00859A22+2655074] GetHandleVerifier [0x0084CA24+2601828] GetHandleVerifier [0x00668C0A+619850] GetHandleVerifier [0x00667830+614768] (No symbol) [0x005505FC] (No symbol) [0x00555968] (No symbol) [0x00555A55] (No symbol) [0x0056051B] BaseThreadInitThunk [0x74D06939+25] RtlGetFullPathName_UEx [0x76FD8FD2+1218] RtlGetFullPathName_UEx [0x76FD8F9D+1165]

該当のソースコード

python

1from selenium import webdriver 2from selenium.webdriver.chrome.options import Options 3import chromedriver_binary 4 5options = Options() 6PROFILE_PATH = r'C:/Users/userName/AppData/Local/Google/Chrome/User Data/' 7PROFILE_DIR = 'Profile 2' 8options.add_argument('--user-data-dir=' + PROFILE_PATH) 9options.add_argument('--profile-directory=' + PROFILE_DIR) 10# options.add_argument('--headless') 11options.add_argument("--no-sandbox") 12options.binary_location = r"C:/Program Files/Google/Chrome/Application/chrome.exe" 13 14print('処理を開始します') 15 16driver = webdriver.Chrome(options=options) 17 18print('接続しました') 19 20driver.get('https://jp.mercari.com/') 21driver.quit 22

試したこと

下記のオプションを追加

python

1options.add_argument('--headless') 2options.add_argument("--no-sandbox")

これらを適用すれば問題なく動きますが、実際に動きを確認したいため--headlessは望ましくないです。

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

Google Chrome バージョン
108.0.5359.125

chromedriver バージョン
108.0.5359.71

環境構築はAnacondaを利用しました。
コードを実行する際、該当のプロファイルを使用したChromeは起動していません。

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

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

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

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

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

guest

回答1

0

このエラーは引数が正常に読み込めなかった時に起きます。今回はパスの指定方法が間違っていると思われます。

パスのフォワードスラッシュ / を バックスラッシュ \ にしてください。

python

1PROFILE_PATH = r'C:\Users\userName\AppData\Local\Google\Chrome\User Data'

そして、実行ファイルの指定は値の代入ではなく、メソッドを呼び出して引数として渡してください。

python

1options.binary_location(r"C:\Program Files\Google\Chrome\Application\chrome.exe")

投稿2022/12/26 19:50

Demerara

総合スコア397

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

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

hello_water

2022/12/26 22:46

ご回答ありがとうございます。 ご教授頂いた内容のとおりにコードを修正した場合、下記のようなエラーが発生します。 素人質問で恐縮ですが、文字列として値を渡しているのにTypeErrorが起きる理由が分かりません。 ご存知でしたら重ねてご教授いただけないでしょうか。 ```python Traceback (most recent call last): File "D:\Docs\Python\mercariSeleniumAutomation\Setting.py", line 12, in <module> options.binary_location(r"C:\Program Files\Google\Chrome\Application\chrome.exe") TypeError: 'str' object is not callable ``` ```python 修正後 from selenium import webdriver from selenium.webdriver.chrome.options import Options import chromedriver_binary options = Options() PROFILE_PATH = r'C:\Users\userName\AppData\Local\Google\Chrome\User Data' PROFILE_DIR = 'Profile 2' options.add_argument('--user-data-dir=' + PROFILE_PATH) options.add_argument('--profile-directory=' + PROFILE_DIR) # options.add_argument('--headless') options.add_argument("--no-sandbox") options.binary_location(r"C:\Program Files\Google\Chrome\Application\chrome.exe") print('処理を開始します') driver = webdriver.Chrome(options=options) print('接続しました') driver.get('https://jp.mercari.com/') driver.quit ```
Demerara

2022/12/27 01:27

selenium のバージョンは、4.0 以上ですか? だとすれば、Options() ではなく、webdriver.ChromeOptions() としてみてください。
hello_water

2022/12/27 11:58

お世話になっております。 Seleniumのバージョンは4.7.2でした。 options = Options() 上記のコードを下記に変更しましたが、発生している問題・エラーメッセージにて掲載したエラーと同様のものが発生いたしました。 options = webdriver.ChromeOptions()
Demerara

2022/12/27 12:40

それはおかしいですね。selenium 4.7.2 には、確かに binary を指定するメソッドが実装されています。それが、呼び出せずプロパティとして指定する方法しかないと言われているということですね。環境内に複数の selenium があって別の selenium が動いているとかそういうことはありませんか? 一度試しに、当初の方法で binary を指定してみてください。エラーの内容に変化はあるでしょうか? `options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe"`
hello_water

2022/12/27 13:07

お世話になっております。 >環境内に複数の selenium があって別の selenium が動いているとかそういうことはありませんか? 確認のため一度seleniumをアンインストールし、他の環境でもないか確認しましたがありませんでした。 再インストールし実行したところ同様のエラーが発生しました。 以下実行時のコードとエラーになります。 ``` エラーメッセージ (mercari) D:\Docs\Python\mercariSeleniumAutomation>python Setting.py 処理を開始します �����̃u���E�U �Z�b�V�����ŊJ���Ă��܂��B Traceback (most recent call last): File "D:\Docs\Python\mercariSeleniumAutomation\Setting.py", line 17, in <module> driver = webdriver.Chrome(options=options) File "C:\Users\userName\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__ super().__init__( File "C:\Users\userName\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 106, in __init__ super().__init__( File "C:\Users\userName\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 288, in __init__ self.start_session(capabilities, browser_profile) File "C:\Users\userName\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 381, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "C:\Users\userName\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 444, in execute self.error_handler.check_response(response) File "C:\Users\userName\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 249, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited normally. (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location C:\Program Files\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.) Stacktrace: Backtrace: (No symbol) [0x00DDF243] (No symbol) [0x00D67FD1] (No symbol) [0x00C5D04D] (No symbol) [0x00C7C24E] (No symbol) [0x00C782E9] (No symbol) [0x00CAF056] (No symbol) [0x00CAEB2A] (No symbol) [0x00CA8386] (No symbol) [0x00C8163C] (No symbol) [0x00C8269D] GetHandleVerifier [0x01079A22+2655074] GetHandleVerifier [0x0106CA24+2601828] GetHandleVerifier [0x00E88C0A+619850] GetHandleVerifier [0x00E87830+614768] (No symbol) [0x00D705FC] (No symbol) [0x00D75968] (No symbol) [0x00D75A55] (No symbol) [0x00D8051B] BaseThreadInitThunk [0x77606939+25] RtlGetFullPathName_UEx [0x77E78FD2+1218] RtlGetFullPathName_UEx [0x77E78F9D+1165] ```
Demerara

2022/12/27 13:38 編集

やはりオプションの指定内容に問題があるため、当初と同一のエラーが返ってきてるようです。 次のコードを同じ環境内の別の新しいファイルに保存して実行してみください。ファイル名は、他のモジュールと被ることがないように、`mercari_scraper_test.py` とでもしましょう。 こちらのコードは手元の環境で問題なく動作している内容です。 ```python import chromedriver_binary from selenium import webdriver # User の部分は自身のユーザー名に書き換えてください PROFILE_PATH = "C:\\Users\\User\\AppData\\Local\\Google\\Chrome\\User Data" PROFILE_DIR = "Profile 2" options = webdriver.ChromeOptions() options.binary_location = ( "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" ) options.add_argument("user-data-dir=" + PROFILE_PATH) options.add_argument("profile-directory=" + PROFILE_DIR) driver = webdriver.Chrome(options=options) driver.get("https://jp.mercari.com/") driver.implicitly_wait(5) driver.quit() ``` それぞれのパスに、間違いなく該当のフォルダーが存在していることを確認してください。
Demerara

2022/12/27 14:39 編集

すみません、手元の環境で動いたのは一度だけでした。どうやら、chromedriver_binary というパッケージが使えないようです。 そちらのモジュールを削除して、正しい場所(https://chromedriver.chromium.org/downloads)から現在使用している Chrome と同一のバージョンの chromedriver をダウンロードしてください。 そして、以下のようにサービスのインスタンスを作成し、ドライバーの引数に渡してください。 ```python import time from selenium import webdriver from selenium.webdriver.chrome.service import Service userDir = "C:\\Users\\User\\AppData\\Local\\Google\\Chrome\\User Data" profileDir = "Profile 1" options = webdriver.ChromeOptions() options.add_argument("user-data-dir=" + userDir) options.add_argument("profile-directory=" + profileDir) service = Service(executable_path="C:\\Users\\User\\bin\\chromedriver\\chromedriver.exe") driver = webdriver.Chrome(service=service, options=options) driver.get("https://jp.mercari.com/") time.sleep(2) print(driver.title) time.sleep(2) driver.quit() ```
hello_water

2022/12/27 21:56

お世話になっております。 一度Chromeドライバーを削除して、再度同様のバージョン(今回ですとChromeが108.0.5359.125ですので108.0.5359.71をインストールし下記に格納しました) ``` cmd C:\Users\user\bin\chromedriver のディレクトリ 2022/12/28 06:49 <DIR> . 2022/12/28 06:49 <DIR> .. 2022/11/29 02:35 12,358,144 chromedriver.exe ``` しかしながら同様のエラーが発生しております。 ```python import time from selenium import webdriver from selenium.webdriver.chrome.service import Service userDir = "C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data" profileDir = "Profile 1" options = webdriver.ChromeOptions() options.add_argument("user-data-dir=" + userDir) options.add_argument("profile-directory=" + profileDir) service = Service(executable_path="C:\\Users\\user\\bin\\chromedriver\\chromedriver.exe") driver = webdriver.Chrome(service=service, options=options) driver.get("https://jp.mercari.com/") time.sleep(2) print(driver.title) time.sleep(2) driver.quit() Traceback (most recent call last): File "D:\Docs\Python\mercariSeleniumAutomation\Setting.py", line 15, in <module> driver = webdriver.Chrome(service=service, options=options) File "C:\Users\user\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__ super().__init__( File "C:\Users\user\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 106, in __init__ super().__init__( File "C:\Users\user\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 288, in __init__ self.start_session(capabilities, browser_profile) File "C:\Users\user\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 381, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "C:\Users\user\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 444, in execute self.error_handler.check_response(response) File "C:\Users\user\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 249, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited normally. (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location C:\Program Files\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.) Stacktrace: Backtrace: (No symbol) [0x0069F243] (No symbol) [0x00627FD1] (No symbol) [0x0051D04D] (No symbol) [0x0053C24E] (No symbol) [0x005382E9] (No symbol) [0x0056F056] (No symbol) [0x0056EB2A] (No symbol) [0x00568386] (No symbol) [0x0054163C] (No symbol) [0x0054269D] GetHandleVerifier [0x00939A22+2655074] GetHandleVerifier [0x0092CA24+2601828] GetHandleVerifier [0x00748C0A+619850] GetHandleVerifier [0x00747830+614768] (No symbol) [0x006305FC] (No symbol) [0x00635968] (No symbol) [0x00635A55] (No symbol) [0x0064051B] BaseThreadInitThunk [0x75BA7D69+25] RtlInitializeExceptionChain [0x7771BB9B+107] RtlClearBits [0x7771BB1F+191] ```
Demerara

2022/12/28 11:43

今気づいたのですが、"selenium\webdriver\remote\errorhandler.py" でエラーが起きてます。common の webdriver ではなく、remote の webdriver のファイルです。 もしかして、サードパーティーアプリによるローカルホストへのアクセスがファイアーウォールやアンチウィルスソフトなどでブロックされていたりしませんか? 確認後、上記のコードに、`options.add_argument("remote-debugging-port=9222")` というオプションを追加してみてください。ポートナンバーは任意の値に設定してください。 もう一つ気になるのは、出力の文字化けです。コマンドプロンプトで実行していますか?で、あればスクリプトを実行する前に、`chcp 65001` と打って文字コードを `utf-8` に変更してから実行してみてください。また、PowerShell で実行した場合に変化はあるかも確認して頂けると助かります。確実に仮想環境をアクティベートした状態で実行するようお願いします。パスが正確なのにも関わらず読み込めていないとしたら理由はここにあるかもしれません。 それと一つ確認忘れなのですが、仮想環境で使用している Python のバージョンを教えてください。3.7 以上であるかが重要です。できれば、3.9 以上が望ましいです。
hello_water

2022/12/30 03:47

お世話になっております。 >もしかして、サードパーティーアプリによるローカルホストへのアクセスがファイアーウォールやアンチウィルスソフトなどでブロックされていたりしませんか? 確認してみます。ありがとうございます。 >`chcp 65001` と打って文字コードを `utf-8` に変更してから実行してみてください。 文字化けはしなくなりました。ありがとうございます。 >また、PowerShell で実行した場合に変化はあるかも確認して頂けると助かります。 powershellでのログを下記に記載します。 ``` python (base) PS D:\Docs\Python\mercariSeleniumAutomation> conda activate mercari (mercari) PS D:\Docs\Python\mercariSeleniumAutomation> python .\AutoReListing.py Traceback (most recent call last): File "D:\Docs\Python\mercariSeleniumAutomation\AutoReListing.py", line 18, in <module> driver = webdriver.Chrome(service=service, options=options) File "C:\Users\NOkud\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__ super().__init__( File "C:\Users\NOkud\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 106, in __init__ super().__init__( File "C:\Users\NOkud\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 288, in __init__ self.start_session(capabilities, browser_profile) File "C:\Users\NOkud\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 381, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "C:\Users\NOkud\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 444, in execute self.error_handler.check_response(response) File "C:\Users\NOkud\anaconda3\envs\mercari\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 249, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited normally. (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location C:\Program Files\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.) Stacktrace: Backtrace: (No symbol) [0x00F9F243] (No symbol) [0x00F27FD1] (No symbol) [0x00E1D04D] (No symbol) [0x00E3C24E] (No symbol) [0x00E382E9] (No symbol) [0x00E6F056] (No symbol) [0x00E6EB2A] (No symbol) [0x00E68386] (No symbol) [0x00E4163C] (No symbol) [0x00E4269D] GetHandleVerifier [0x01239A22+2655074] GetHandleVerifier [0x0122CA24+2601828] GetHandleVerifier [0x01048C0A+619850] GetHandleVerifier [0x01047830+614768] (No symbol) [0x00F305FC] (No symbol) [0x00F35968] (No symbol) [0x00F35A55] (No symbol) [0x00F4051B] BaseThreadInitThunk [0x772D7D69+25] RtlInitializeExceptionChain [0x77BABB9B+107] RtlClearBits [0x77BABB1F+191] ``` >仮想環境で使用している Python のバージョンを教えてください。 3.9~でした。 ``` (mercari) D:\Docs\Python\mercariSeleniumAutomation>python --version Python 3.9.13 ```
hello_water

2022/12/30 03:54

余談ですが下記のサイトを参考にしたら、Profileを指定した場合でも正常に動作しました。 https://groups.google.com/g/seleniumjp/c/HETX_iiYQG4?pli=1 Profileの場所をchrome://versionで表示しているものではなく、新たにCドライブ配下に作成することで別の手段ですが問題は解決しました。 ``` python import time from selenium import webdriver from selenium.webdriver.chrome.service import Service userDir = "C:\\ChromeData\\Google\\Chrome\\User Data" profileDir = "Profile 2" options = webdriver.ChromeOptions() options.add_argument("user-data-dir=" + userDir) options.add_argument("profile-directory=" + profileDir) service = Service(executable_path="C:\\Users\\user\\bin\\chromedriver\\chromedriver.exe") driver = webdriver.Chrome(service=service, options=options) driver.get("https://jp.mercari.com/") time.sleep(2) print(driver.title) time.sleep(2) driver.quit() ``` 解決はしたものの結構強引なやり方なので腑に落ちない状況です。 もう少し調べてみます...
Demerara

2022/12/30 10:46

なるほど、参照先の方が参照した不具合報告板のコメントによると、"User Data" 内のファイルが破損していることに起因するエラーのようですね。そのため、別の場所に "User Data" を配置することで問題を回避していると。 となると、正しい解決方法は Chrome のアンインストールと再インストールかと思います。その際、"Local\Google\Chrome" と "Roaming\Google\Chrome" を確実に削除してください。キャッシュが残っているとそこから読み込んで以前の環境を再現してしまいます。このフォルダーを削除するとブラウザのデータは全て消えるので、パスワード等を保存しているならバックアップを取っておいてください。また、アンインストールしたにも関わらず、"C:\Program Files\Google\Chrome" 内にファイルが残っていたりするので、それも削除してください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問