4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

webdriver.Chrome()のエラー

Last updated at Posted at 2024-01-13

いまにゅさんの動画()
いまにゅさんのスクレイピング講義動画を見ながら、初めてスクレイピングをハンズオンで進めたところ、エラーが起きてしまいました。

初心者用の動画なので、他の方が躓いたときの参考になれば幸いです。

実行したのは下記コードです。

!pip install webdriver_manager
from selenium import webdriver
browser = webdriver.Chrome(ChromeDriverManager().install())

エラーコードは下記です。

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File c:\Users\037ta\jupyter_lab\venv\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 c:\Users\037ta\jupyter_lab\venv\Lib\site-packages\selenium\webdriver\common\selenium_manager.py:84, in SeleniumManager.driver_location(self, options)
     77 """Determines the path of the correct driver.
     78 
     79 :Args:
     80  - browser: which browser to get the driver path for.
     81 :Returns: The driver path to use
     82 """
---> 84 browser = options.capabilities["browserName"]
     86 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[20], line 1
----> 1 browser = webdriver.Chrome(ChromeDriverManager().install())
...
---> 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'
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

AttributeError: 'str' object has no attribute 'capabilities' 

この部分が、Selenium WebDriverの初期化における問題を示しているらしく、他にもOptionについてのエラーも書かれています。

下記のように、修正すると無事実行でき、ブラウザが立ち上がりました。

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

options = webdriver.ChromeOptions()
# options.add_argument('--headless')  #ヘッドレスモードを使用する場合は有効にします
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

# browser = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
browser = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?