Selenium で Chrome を複数起動する!
既に自動運転を稼働させてる環境で別の自動運転ソフトを動かそうとしたら、Chromeが起動できませんでした。今回は、別々のSeleniumを使ったソフトで、複数のChromeを起動できるようにするメモです。
まず結論、対処方法
同一のユーザーデータディレクトリで、複数のChromeDriverを操作することはできないようです。同時に2個以上のChromeDriverを操作する場合は、–user-data-dirの指定を分ければ大丈夫。各仮想環境の配下にフォルダーを用意して使い分けるのがわかりやすいかな。
# ----------- chrome webDriver ----------- # # 自動で最新バージョンをダウンロードしてパス名を返してくれる。 driver_path = ChromeDriverManager().install() # chrome options options = webdriver.ChromeOptions() # このプロファイル設定しているところのuserDirが実行中のアプリと重複してたら起動できない # userDir = r'C:\Users\xxx\AppData\Local\Google\Chrome\User Data\Default' # ということで、仮想環境下にChromeProfileフォルダを準備して指定しておく userDir = r'C:\PyProj\venv\ChromeProfile' profileDir = 'Profile 1' options.add_argument('--user-data-dir=' + userDir) options.add_argument('--profile-directory=' + profileDir) #options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument('--disable-gpu') options.add_argument('--ignore-certificate-errors') # make chrome log requests capabilities = DesiredCapabilities.CHROME # caps['goog:loggingPrefs'] capabilities["goog:loggingPrefs"] = {"performance": "ALL"} # chrome service service = Service(executable_path=driver_path) self.driver = webdriver.Chrome( desired_capabilities= capabilities, service = service, options = options )
重複した場合のエラー
重複した場合、以下のようなエラーで起動できません。
既存のブラウザ セッションで開いています。 Traceback (most recent call last): File "c:\PyProj\xxxx\test.py", line 44, in module driver = webdriver.Chrome( File "c:\PyProj\xxxx\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__ super().__init__( File "c:\PyProj\xxxx\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 61, in __init__ super().__init__(command_executor=executor, options=options) File "c:\PyProj\xxxx\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 208, in __init__ self.start_session(capabilities) File "c:\PyProj\xxxx\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 292, in start_session response = self.execute(Command.NEW_SESSION, caps)["value"] File "c:\PyProj\xxxx\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute self.error_handler.check_response(response) File "c:\PyProj\xxxx\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.SessionNotCreatedException: Message: session not created: Chrome failed to start: exited normally. (session not created: 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.)
補足:profileDirを分けてもダメ
最初は、profileDirのほうの設定を変えたらいいと思ってましたがダメでした。フォルダーを変える必要があります。
コメント
0 件のコメント :
コメントを投稿