python+selenium+firefox加载配置文件,无法自动启用插件的问题

path\firefox.exe -p
新建配置文件夹

在这里插入图片描述
安装好插件,禁止插件自动更新,调整页面至想要的状态。
关闭firefox,配置文件已自动保存

python中

from selenium import webdriver
fp = webdriver.FirefoxOptions()
fp.binary_location = r"..\Firefox\App\Firefox\firefox.exe"
fp.profile = r"..\firefox-profiles-dev"
# fp.set_preference("extensions.pendingOperations", True)
driver = webdriver.Firefox(options=fp)

插件虽设置已启用,但实际未启动!
在这里插入图片描述
关闭打开后,才实际启用
在这里插入图片描述
复制了一份配置文件到TEMP文件夹,文件夹自动生成,每次名称不一致
在这里插入图片描述

在这种状态中做的所有设置,都被保存在临时文件中,下次不会再加载。

分析:
在selenium拉起 friefox中,关闭开启插件,配置文件夹中发现有以下三个文件有变动
在这里插入图片描述
在这里插入图片描述
prefs.js中user_pref键值对可以用 webdriver.FirefoxOptions的set_preference方法设置。

selenium4 有提示 “DeprecationWarning: Setting a profile has been deprecated. Please use the set_preference and install_addons methods”,不建议使用配置文件,转而使用“set_preference”来设置firefox属性
prefs.js 中的键值对可用来做设置参考

比较插件重新启用前后的prefs.js
有一处与插件(extensions)相关
在这里插入图片描述
"extensions.pendingOperations"之前为false,重启插件后为true

python中添加一句

fp.set_preference("extensions.pendingOperations", True)

完整:

from selenium import webdriver
fp = webdriver.FirefoxOptions()
fp.binary_location = r"..\Firefox\App\Firefox\firefox.exe"
fp.profile = r"..\firefox-profiles-dev"
fp.set_preference("extensions.pendingOperations", True)
driver = webdriver.Firefox(options=fp)

自动加载插件成功
在这里插入图片描述

分析原理
https://wiki.mozilla.org/Extension_Manager:API_Rewrite
在这里插入图片描述

Some types of add-ons may require restarts for certain operations. XPI style extensions for example require restarts for almost all operations. The API exposes the operations that are pending in the pendingOperations property. Pending operations can make it hard to tell what state an actual add-on is in. To help solve this the isActive property indicates whether the add-on is currently active. This is separate to and may be different to the various userDisabled, isCompatible and other properties that indicate whether an add-on can be active or not.

估计是xpi形式的插件会被挂起。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值