祝大家有美好的一天,
我正在制作一个程序来在 Chrome 中打开特定页面,当调用“chromedriver”时,我收到以下错误:
`from selenium import webdriver
driver = webdriver.Chrome('webdriver//chromedriver.exe')`
--------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[4], line 1
----> 1 driver = webdriver.Chrome('webdriver//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 browser_name=DesiredCapabilities.CHROME["browserName"],
47 vendor_prefix="goog",
48 options=options,
49 service=service,
50 keep_alive=keep_alive,
51 )
File ~\AppData\Local\Programs\Python\Python312\Lib\site- packages\selenium\webdriver\chromium\webdriver.py:50, in ChromiumDriver.__init__(self, browser_name, vendor_prefix, options, service, keep_alive)
47 self.service = service
49 finder = DriverFinder(self.service, options)
---> 50 if finder.get_browser_path():
51 options.binary_location = finder.get_browser_path()
52 options.browser_version = None
File ~\AppData\Local\Programs\Python\Python312\Lib\site- packages\selenium\webdriver\common\driver_finder.py:47, in DriverFinder.get_browser_path(self)
46 def get_browser_path(self) -> str:
---> 47 return self._binary_paths()["browser_path"]
File ~\AppData\Local\Programs\Python\Python312\Lib\site- packages\selenium\webdriver\common\driver_finder.py:56, in DriverFinder._binary_paths(self)
53 if self._paths["driver_path"]:
54 return self._paths
---> 56 browser = self._options.capabilities["browserName"]
57 try:
58 path = self._service.path
AttributeError: 'str' object has no attribute 'capabilities'
como puedo solucionar este error, agradezco mucho su colaboración.
您没有正确实现 chrome 驱动程序二进制文件的位置。
试试这个。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.binary_location = rb"C:/chromedriver/chromedriver.exe"
driver = webdriver.Chrome(options=options)
将二进制文件的位置更改为您的 chrome 驱动程序所在的位置。