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

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

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

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

Python 3.x

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

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

selenium

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

Q&A

解決済

1回答

2899閲覧

Python/Selenium/ChromedriverによるWEBスクレイピングでGoogleChromeを開けない

abcdtaichi

総合スコア18

スクレイピング

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

Python 3.x

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

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

selenium

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

0グッド

0クリップ

投稿2023/10/23 13:00

前提

以下のYouTubeにてWEBスクレイピングの勉強をしています。

https://www.youtube.com/watch?v=VRFfAeW30qE&lc=UgwlkYQ_IuC_Y11aIeB4AaABAg.9K8rGLlJNID9KGSMi2LGJd

しかし、ブラウザを立ち上げるコードを実行しようとすると、長文のエラーが出てきてしまい、躓いている状況です。
もし、以下のエラーがどういったもので、どうすれば解決するのかがわかる方がいらっしゃったらご教示いただきたいです。

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

長文のエラーというのが以下になります。

--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\common\driver_finder.py:38, in DriverFinder.get_path(service, options) 37 try: ---> 38 path = SeleniumManager().driver_location(options) if path is None else path 39 except Exception as err: File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\common\selenium_manager.py:79, in SeleniumManager.driver_location(self, options) 72 """Determines the path of the correct driver. 73 74 :Args: 75 - browser: which browser to get the driver path for. 76 :Returns: The driver path to use 77 """ ---> 79 browser = options.capabilities["browserName"] 81 args = [str(self.get_binary()), "--browser", browser] AttributeError: 'str' object has no attribute 'capabilities' During handling of the above exception, another exception occurred: AttributeError Traceback (most recent call last) Cell In[11], line 1 ----> 1 browser = webdriver.Chrome('chromedriver.exe') File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\chrome\webdriver.py:45, in WebDriver.__init__(self, options, service, keep_alive) 42 service = service if service else Service() 43 options = options if options else Options() ---> 45 super().__init__( 46 DesiredCapabilities.CHROME["browserName"], 47 "goog", 48 options, 49 service, 50 keep_alive, 51 ) File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\chromium\webdriver.py:51, in ChromiumDriver.__init__(self, browser_name, vendor_prefix, options, service, keep_alive) 47 self.vendor_prefix = vendor_prefix 49 self.service = service ---> 51 self.service.path = DriverFinder.get_path(self.service, options) 53 self.service.start() 55 try: File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\common\driver_finder.py:40, in DriverFinder.get_path(service, options) 38 path = SeleniumManager().driver_location(options) if path is None else path 39 except Exception as err: ---> 40 msg = f"Unable to obtain driver for {options.capabilities['browserName']} using Selenium Manager." 41 raise NoSuchDriverException(msg) from err 43 if path is None or not Path(path).is_file(): AttributeError: 'str' object has no attribute 'capabilities'

該当のソースコード

Python

1from selenium import webdriver 2from time import sleep 3browser = webdriver.Chrome('chromedriver.exe')

試したこと

動画内でWindowsでエラーが出た時の対策としてwebdriver_managerを使用する方法が紹介されていたのですが、こちらでも同様に長文のエラーが出てきました。

Python

1from webdriver_manager.chrome import ChromeDriverManager 2browser = webdriver.Chrome(ChromeDriverManager().install())

Python

1from webdriver_manager.chrome import ChromeDriverManager 2browser = webdriver.Chrome(ChromeDriverManager().install())
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\common\driver_finder.py:38, in DriverFinder.get_path(service, options) 37 try: ---> 38 path = SeleniumManager().driver_location(options) if path is None else path 39 except Exception as err: File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\common\selenium_manager.py:79, in SeleniumManager.driver_location(self, options) 72 """Determines the path of the correct driver. 73 74 :Args: 75 - browser: which browser to get the driver path for. 76 :Returns: The driver path to use 77 """ ---> 79 browser = options.capabilities["browserName"] 81 args = [str(self.get_binary()), "--browser", browser] AttributeError: 'str' object has no attribute 'capabilities' During handling of the above exception, another exception occurred: AttributeError Traceback (most recent call last) Cell In[12], line 1 ----> 1 browser = webdriver.Chrome(ChromeDriverManager().install()) File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\chrome\webdriver.py:45, in WebDriver.__init__(self, options, service, keep_alive) 42 service = service if service else Service() 43 options = options if options else Options() ---> 45 super().__init__( 46 DesiredCapabilities.CHROME["browserName"], 47 "goog", 48 options, 49 service, 50 keep_alive, 51 ) File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\chromium\webdriver.py:51, in ChromiumDriver.__init__(self, browser_name, vendor_prefix, options, service, keep_alive) 47 self.vendor_prefix = vendor_prefix 49 self.service = service ---> 51 self.service.path = DriverFinder.get_path(self.service, options) 53 self.service.start() 55 try: File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\common\driver_finder.py:40, in DriverFinder.get_path(service, options) 38 path = SeleniumManager().driver_location(options) if path is None else path 39 except Exception as err: ---> 40 msg = f"Unable to obtain driver for {options.capabilities['browserName']} using Selenium Manager." 41 raise NoSuchDriverException(msg) from err 43 if path is None or not Path(path).is_file(): AttributeError: 'str' object has no attribute 'capabilities'

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

Windows 11 Home
Python 3.12.0
Google Chrome バージョン: 118.0.5993.89(Official Build) (64 ビット)

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

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

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

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

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

guest

回答1

0

ベストアンサー

エラーの解説をしようかと思いましたが、少し前に書いたのでそのコピペで。
https://teratail.com/questions/jl8lfxdo8wn0am#reply-8hahcg144t5rgq

chromedriverの場所の指定方法はSeleniumのバージョンでころころ変わって、古いコードを真似るとエラーになるという事態の解決のためか、ver4.6でとうとう指定不要になりました。Selenium自体がブラウザーに合ったバージョンのchromedriver.exeを自動的にダウンロードしてくれますので、自分でダウンロードしたchromedriver.exeは削除しておきましょう。もしver4.6より古いSeleniumを使っているなら最新バージョンに入れ替えましょう。

簡単に言うと古いバージョン用のコードを使おうとしたのでエラーです。Seleniumは割と変化が早いので、他人の記事や動画を見る場合はまずSeleniumのバージョンを確認しましょう。

結論だけ書くと下記のコードに変えましょう。

Python

1from selenium import webdriver 2from time import sleep 3browser = webdriver.Chrome() 4~~~

投稿2023/10/23 13:19

otn

総合スコア86295

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

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

abcdtaichi

2023/10/23 14:29

ご回答いただきありがとうございました。 chromedriverを削除後にご共有いただいたコードを試したら、成功しました。 迅速かつ正確にご回答いただきましてありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.31%

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

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

質問する

関連した質問