我试图使用Selenium连接到现有的Firefox实例-文档说使用这样的东西

options=webdriver.FirefoxOptions()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'


webdriver_service = Service(r'c:\tmp\geckodriver.exe')
driver = webdriver.Firefox(service = webdriver_service, service_args=['--marionette-port', '2828', '--connect-existing'])

然而,我得到了一个错误

    driver = webdriver.Firefox(service = webdriver_service, service_args=['--marionette-port', '2828', '--connect-existing'])
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: WebDriver.__init__() got an unexpected keyword argument 'service_args'

我看到了关于"意外关键字参数"的其他问题,他们说最新版本的Selify有通过选项传递参数的其他方法.

我试过了

options.add_argument('--marionette-port')
options.add_argument('2828') 
options.add_argument('--connect-existing')

但它似乎仍在创造一个新的Firefox实例

我用以下理由启动了Firefox

"C:\Program Files\Mozilla Firefox\Firefox.exe"-Marionette-启动-调试器-服务器2828

我该怎么弥补?


这些是我的版本

Python版本

python --version
Python 3.12.2

selenium版

pip show selenium
Name: selenium
Version: 4.18.1

Gecko驱动程序版本

geckodriver --version
geckodriver 0.34.0 (c44f0d09630a 2024-01-02 15:36 +0000)

Firefox 123.0(64位)

Windows 11

推荐答案

你应该把service_args参数放在Service类中,而不是放在Firefox类中.我相信这应该行得通

options=webdriver.FirefoxOptions()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'


webdriver_service = Service(r'c:\tmp\geckodriver.exe', service_args=['--marionette-port', '2828', '--connect-existing'])
driver = webdriver.Firefox(service = webdriver_service)

Python相关问答推荐

如何自动抓取以下CSV

如何使用html从excel中提取条件格式规则列表?

更改键盘按钮进入'

Python库:可选地支持numpy类型,而不依赖于numpy

Python,Fitting into a System of Equations

基于索引值的Pandas DataFrame条件填充

我想一列Panadas的Rashrame,这是一个URL,我保存为CSV,可以直接点击

字符串合并语法在哪里记录

Pandas GroupBy可以分成两个盒子吗?

通过ManyToMany字段与Through在Django Admin中过滤

在不同的帧B中判断帧A中的子字符串,每个帧的大小不同

将标签移动到matplotlib饼图中楔形块的开始处

如何过滤组s最大和最小行使用`transform`'

Python—在嵌套列表中添加相同索引的元素,然后计算平均值

为罕见情况下的回退None值键入

我怎么才能用拉夫分拣呢?

分解polars DataFrame列而不重复其他列值

时间戳上的SOAP头签名无效

为什么这个正则表达式没有捕获最后一次输入?

如何将参数名作为参数传入到函数中?