I've just started with Selenium and I'm already stuck at the first step: setting up the driver.
I keep getting this error:

‘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'

我正在对Python3.11使用VS代码,如果这能有所帮助的话.

推荐答案

这是由于selenium 4.10.0中的变化: https://github.com/SeleniumHQ/selenium/commit/9f5801c82fb3be3d5850707c46c3f8176e3ccd8e

Changes_in_selenium_4_10_0

请注意,第一个参数不再是executable_path,而是options.(ChromeDriverManager().install()返回安装位置的路径.)由于Selify Manager现在包含在selenium 4.10.0中,因此您不应该再使用ChromeDriverManager.

from selenium import webdriver

driver = webdriver.Chrome()
# ...
driver.quit()

但是,如果您仍想将executable_path传递给现有的驱动程序,则必须立即使用service参数:

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

Python相关问答推荐

Python中的锁定类和线程以实现dict移动

如何使用关键参数按列对Pandas rame进行排序

合并同名列,但一列为空,另一列包含值

仅对matplotlib的条标签中的一个条标签应用不同的格式

在应用循环中间保存pandas DataFrame

Pandas :多索引组

将整组数组拆分为最小值与最大值之和的子数组

标题:如何在Python中使用嵌套饼图可视化分层数据?

在Mac上安装ipython

如何在给定的条件下使numpy数组的计算速度最快?

如果值发生变化,则列上的极性累积和

Django REST Framework:无法正确地将值注释到多对多模型,不断得到错误字段名称字段对模型无效'<><>

导入...从...混乱

当点击tkinter菜单而不是菜单选项时,如何执行命令?

在www.example.com中使用`package_data`包含不包含__init__. py的非Python文件

处理具有多个独立头的CSV文件

在输入行运行时停止代码

干燥化与列姆化的比较

为什么在FastAPI中创建与数据库的连接时需要使用生成器?

使用python playwright从 Select 子菜单中 Select 值