我刚刚开始使用 Selenium,但我已经陷入了第一步:设置驱动程序。
我不断收到此错误:
“str”对象没有属性“_ignore_local_proxy”。
这是代码:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import requests
driver = webdriver.Chrome(ChromeDriverManager().install())
以及整个回溯:
AttributeError Traceback (most recent call last)
Cell In[21], line 1
----> 1 driver = webdriver.Chrome(ChromeDriverManager().install())
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\chrome\webdriver.py:49, in WebDriver.__init__(self, options, service, keep_alive)
45 self.keep_alive = keep_alive
47 self.service.path = DriverFinder.get_path(self.service, self.options)
---> 49 super().__init__(
50 DesiredCapabilities.CHROME["browserName"],
51 "goog",
52 self.options,
53 self.service,
54 self.keep_alive,
55 )
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\chromium\webdriver.py:60, in ChromiumDriver.__init__(self, browser_name, vendor_prefix, options, service, keep_alive)
51 self.service.start()
53 try:
54 super().__init__(
55 command_executor=ChromiumRemoteConnection(
56 remote_server_addr=self.service.service_url,
57 browser_name=browser_name,
58 vendor_prefix=vendor_prefix,
59 keep_alive=keep_alive,
...
63 )
64 except Exception:
65 self.quit()
AttributeError: 'str' object has no attribute '_ignore_local_proxy'
我正在使用 VS Code 和 Python 3.11,如果这能有所帮助的话。
这是由于
selenium
4.10.0
的变化所致:
https://github.com/SeleniumHQ/selenium/commit/9f5801c82fb3be3d5850707c46c3f8176e3ccd8e
请注意,第一个参数不再是
executable_path
,而是 options
。 (ChromeDriverManager().install()
返回安装位置的路径。)由于selenium管理器现在包含在selenium
4.10.0
中,您根本不应该再使用ChromeDriverManager
。
from selenium import webdriver
driver = webdriver.Chrome()
# ...
driver.quit()
但是,如果您仍然想将
executable_path
传递给现有驱动程序,则现在必须使用 service
arg:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service(executable_path="PATH_TO_DRIVER")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# ...
driver.quit()