Seleniumでプロフィールを指定したうえで任意のURLを開きたい
解決したいこと
Seleniumでchromeのプロフィールを指定したうえで任意のURLを開きたいです。
発生している問題・エラー
下記のようなコードで、プロフィールxxを指定してchromeを起動させようと試みています。
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.service import Service # ←新規追加
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
#from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup as bs
import datetime
from selenium.webdriver.common.by import By
import requests
import lxml
import re
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--user-data-dir=C:\\Users\\xxxxx\\AppData\\Local\\Google\\Chrome\\User Data')
options.add_argument('--profile-directory=Profile xx')
chrome_path = r'C:\Users\yuhta\scraping-book\chromedriver'
service = Service(executable_path=chrome_path)
##options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
url = 'https://yahoo.co.jp'
driver.get(url)
sleep(3)
これで起動すると下記画像のように自分の望んだプロフィールで起動はできているのですが、指定したURLを開くことができていません。
options = Options()
options.add_argument('--user-data-dir=C:\\Users\\xxxxx\\AppData\\Local\\Google\\Chrome\\User Data')
options.add_argument('--profile-directory=Profile xx')
この部分をコメントアウトして起動すると、新規プロフィールで指定したURLが開けています。
つまり、optionの指定の仕方がよくないのかなと自分なりに推察はしているのですが解決できていません。
エラーコード
>>> driver = webdriver.Chrome(service=service, options=options)# ←新規追加
既存のブラウザ セッションで開いています。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 80, in __init__
super().__init__(
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 104, in __init__
super().__init__(
File "C:\Users\xxxxxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 286, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 378, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, 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\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Stacktrace:
GetHandleVerifier [0x00007FF6E84DAD32+56930]
(No symbol) [0x00007FF6E844F632]
(No symbol) [0x00007FF6E83042E5]
(No symbol) [0x00007FF6E8334628]
(No symbol) [0x00007FF6E83305D4]
(No symbol) [0x00007FF6E837595F]
(No symbol) [0x00007FF6E83754C0]
(No symbol) [0x00007FF6E836BA43]
(No symbol) [0x00007FF6E833D438]
(No symbol) [0x00007FF6E833E4D1]
GetHandleVerifier [0x00007FF6E8856ABD+3709933]
GetHandleVerifier [0x00007FF6E88AFFFD+4075821]
GetHandleVerifier [0x00007FF6E88A818F+4043455]
GetHandleVerifier [0x00007FF6E8579766+706710]
(No symbol) [0x00007FF6E845B90F]
(No symbol) [0x00007FF6E8456AF4]
(No symbol) [0x00007FF6E8456C4C]
(No symbol) [0x00007FF6E8446904]
BaseThreadInitThunk [0x00007FFE02FB7344+20]
RtlUserThreadStart [0x00007FFE04EC26B1+33]
>>> url = 'https://yahoo.co.jp'
>>> driver.get(url)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 449, in get
self.execute(Command.GET, {"url": url})
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
(Session info: chrome=122.0.6261.129)
Stacktrace:
GetHandleVerifier [0x00007FF6E84DAD32+56930]
(No symbol) [0x00007FF6E844F632]
(No symbol) [0x00007FF6E83042E5]
(No symbol) [0x00007FF6E82E1D4C]
(No symbol) [0x00007FF6E83723F7]
(No symbol) [0x00007FF6E8387891]
(No symbol) [0x00007FF6E836BA43]
(No symbol) [0x00007FF6E833D438]
(No symbol) [0x00007FF6E833E4D1]
GetHandleVerifier [0x00007FF6E8856ABD+3709933]
GetHandleVerifier [0x00007FF6E88AFFFD+4075821]
GetHandleVerifier [0x00007FF6E88A818F+4043455]
GetHandleVerifier [0x00007FF6E8579766+706710]
(No symbol) [0x00007FF6E845B90F]
(No symbol) [0x00007FF6E8456AF4]
(No symbol) [0x00007FF6E8456C4C]
(No symbol) [0x00007FF6E8446904]
BaseThreadInitThunk [0x00007FFE02FB7344+20]
RtlUserThreadStart [0x00007FFE04EC26B1+33]
>>> sleep(3)
自分で試したこと
chromeが多重起動していないか、chrome driverのバージョンなどを確認しましたが解決策がわかりません。
よろしくお願いいたします。
0