selenium4 接管已打开的Firefox浏览器

事情起因是需要用selenium去接管已打开的Firefox浏览器,参考了CSDN不少文章,虽然最终能用,但是始终有个警告

from selenium import webdriver


FIREFOX_PATH = r'"C:\Program Files\Mozilla Firefox\firefox.exe"'
GECKODRIVER_PATH = r'D:\soft\geckodriver.exe'
driver = webdriver.Firefox(executable_path=GECKODRIVER_PATH,
                           service_args=['--marionette-port', '2828', '--connect-existing'])
print(driver.title)
警告提示:

DeprecationWarning: service_args has been deprecated, please pass in a Service object
  driver = webdriver.Firefox(executable_path=GECKODRIVER_PATH,

大致意思是:弃用警告:service_args已弃用,请传入一个服务对象。

查了不少资料,包括chatgpt3.5,都不能解决问题,猜测是因为我的selenium已经是4.0版本了,网上现有的资料已经没更新了。

通过翻阅资料,按函数提示逐步尝试,终于解决了问题;

selenium4 接管已打开的Firefox浏览器步骤如下:

1、下载geckodriver.exe;注意geckodriver.exe路径与代码的要对应!

2、桌面Firefox快捷方式增加 -marionette -start-debugger-server 2828;

代码如下:

from selenium import webdriver
from selenium.webdriver.firefox.service import Service


GECKODRIVER_PATH = r'D:\soft\geckodriver.exe'
service = Service(executable_path=GECKODRIVER_PATH, service_args=['--marionette-port', '2828', '--connect-existing'])
driver = webdriver.Firefox(service=service)
print(driver.title)

第一次写个文章,仅作纪念!

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值