LoginSignup
yuhtaryouko
@yuhtaryouko (Yuta Kato)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

seleniumでchromeのプロフィールを指定して起動すると指定のURLを開かなくなる

解決したいこと

seleniumを用いて対象URLを開きたいのですが、プロフィールを指定するとただchromeが起動するだけで設定したURLを開いてくれません。

発生している問題・エラー

!pip install selenium
from time import sleep 
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options


import requests 
from bs4 import BeautifulSoup as bs

chrome_path  = r'C:\Users\xxxxx\scraping-book\chromedriver'

options = webdriver.ChromeOptions()
user_dir = r'C:\Users\xxxxx\AppData\Local\Google\Chrome\User Data'
profile_dir = 'Profile 11'
options.add_argument('--user-data-dir=' + user_dir)
options.add_argument('--profile-directory=' + profile_dir)

driver = webdriver.Chrome(executable_path=chrome_path, options=options)

wait = WebDriverWait(driver=driver, timeout=30)

url = 'https://www.xxxx.co.jp' 

driver.get(url)

wait.until(EC.presence_of_all_elements_located)

res = driver.page_source

soup = bs(res, 'html.parser')




image.png

エラーコードは以下の通りです。

WebDriverException                        Traceback (most recent call last)
Input In [11], in <cell line: 10>()
      7 options.add_argument('--profile-directory=' + profile_dir)
      9 ##options.add_extension(r"C:\Users\yuhta\scraping-book\keepa.crx")
---> 10 driver = webdriver.Chrome(executable_path=chrome_path, options=options)

(中略)

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.)

optionsのプロフィールに関する記述を消してこのようなコードにすると設定したURLが表示されます

options = webdriver.ChromeOptions()

##user_dir = r'C:\Users\yuhta\AppData\Local\Google\Chrome\User Data'
##profile_dir = 'Profile 11'
##options.add_argument('--user-data-dir=' + user_dir)
##options.add_argument('--profile-directory=' + profile_dir)


driver = webdriver.Chrome(executable_path=chrome_path, options=options)

wait = WebDriverWait(driver=driver, timeout=30)

url = 'https://www.amazon.co.jp/gp/product/B0BXP67ZW9/' 

driver.get(url)

wait.until(EC.presence_of_all_elements_located)

res = driver.page_source

soup = bs(res, 'html.parser')

image.png

※Amazonがスクレイピングなど禁止なのは理解していますので、そこへの言及はご遠慮ください

自分で試したこと

Chatgptにエラーコードを聞いたら、他のchromeプロセスが起動中ではないかと出たので、同じプロフィールを使っているプロセスは終了した状態でコードを実行しているつもりです。

1

1Answer

「Profile 11」のスペースがいらないかも、とかですかね?
あと質問と関係ないですが、エラー等にユーザーディレクトリ名が表示されている部分があるので気になるようでしたら潰されるのが良いかと思います。

0

Comments

  1. @yuhtaryouko

    Questioner
    ご回答ありがとうございます!

    プロフィールの入っているフォルダ名がスペースを含んでいるので、ここは含んでていても大丈夫かなという気がします。
    プロフィールを読み込んだ状態での起動まではしています。

    get(url)が機能しないという状態ですね。

Your answer might help someone💌