1 回答

TA贡献1851条经验 获得超3个赞
在您的配置文件中有一个文件:prefs.js其中包含一行user_pref("extensions.lastAppBuildId", "20200707180101");对于禁用的插件,该行可能有问题。因此,您可以测试将此数字减 1 或删除整行(未经测试)。
profile.set_preference("extensions.lastAppBuildId", "<apppID> -1 ")
完整示例代码:
from selenium.webdriver import FirefoxProfile
from selenium import webdriver
path = '%APPDATA%\Mozilla\Firefox\Profiles\azk4wioue.default' #path to your profile
profile = FirefoxProfile(path)
profile.set_preference("extensions.lastAppBuildId", "<apppID> -1 ")
driver = webdriver.Firefox(profile)
使用现有 firefox 配置文件的示例:
# go to the the following folder %APPDATA%\Mozilla\Firefox\Profiles\
# there the firefox profiles should be stored, the default one ending with .default
# now provide the profile to the driver like this:
profile = FirefoxProfile('%APPDATA%\Mozilla\Firefox\Profiles\azk4wioue.default')
driver = webdriver.Firefox(firefox_profile=profile)
或者通过临时配置文件在每次运行时安装干净的插件。
# go to https://addons.mozilla.org and search for the plugin you want, e.g.:https://addons.mozilla.org/en-US/firefox/addon/ublock-origin/
# rightclick on the button "add to firefox"
# download the target file to a folder of your choice
# then include the addon like this:
driver.install_addon('/Users/someuser/app/extension.xpi', temporary=True)
或者 2,您可以尝试以这种方式设置扩展名:
from selenium.webdriver import FirefoxProfile
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.add_extension(extension='/Users/someuser/app/extension.xpi')
driver = webdriver.Firefox(profile)
如果插件在加载配置文件后存在但被禁用,您也可以尝试以下操作:
def newTab(fx, url="about:blank"):
wnd = fx.execute(selenium.webdriver.common.action_chains.Command.NEW_WINDOW)
handle = wnd["value"]["handle"]
fx.switch_to.window(handle)
fx.get(url) # changes handle
return fx.current_window_handle
def retoggleAllTheAddons(fx):
initialHandlesCount = len(fx.window_handles)
addonsTabHandle = newTab(fx, "about:addons")
fx.execute_script("""
let hb = document.getElementById("html-view-browser");
let al = hb.contentWindow.window.document.getElementsByTagName("addon-list")[0];
let cards = al.getElementsByTagName("addon-card");
for(let card of cards){
card.addon.disable();
card.addon.enable();
}
""")
if len(fx.window_handles) != 1:
fx.switch_to.window(addonsTabHandle)
fx.close()
添加回答
举报