在 selenium 下启动的浏览器不使用插件

我正在使用 python+selenium,配置文件有插件。在启动时,它会暂时显示它们,但随后它们会被隐藏起来。它们存在,它们没有被禁用,但它们是隐形的并且不起作用。我可以禁用和启用它,然后它会出现在任务栏上并且可以正常工作。

http://img1.mukewang.com/643e4a3300014a5213421069.jpg

使用配置文件手动调用 firefox 时,它可以工作。


这是打印到日志中的内容。


1596129664500   mozrunner::runner   INFO    Running command: "/usr/bin/firefox" "-marionette" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofileO34n0s"

JavaScript error: resource:///modules/sessionstore/SessionStore.jsm, line 1325: uncaught exception: 2147746065

JavaScript error: resource://gre/modules/ExtensionContent.jsm, line 554: TypeError: Argument 1 of PrecompiledScript.executeInGlobal is not an object.

1596129672037   Marionette  INFO    Listening on port 41285

1596129672136   Marionette  WARN    TLS certificate errors will be ignored for this session

JavaScript error: undefined, line 14: Error: An unexpected error occurred

JavaScript error: moz-extension://45aaa1ae-14fe-4a8f-841d-6a9416fd5d09/lib/picture_in_picture_overrides.js, line 15: Error: Incorrect argument types for pictureInPictureParent.setOverrides.

1596129683512   Marionette  INFO    Stopped listening on port 41285

难道是因为这些错误?


代码本身根本没有意义:


#!/usr/bin/env python


from selenium.webdriver import Firefox

from selenium.webdriver.firefox.options import Options


profile_path='./profile'


opts=Options()

opts.profile=profile_path

driver=Firefox(options=opts)


breakpoint()

版本可能更重要:


火狐浏览器 68.9.0esr

壁虎驱动程序 0.26.0 (e9783a644016 2019-10-10 13:38 +0000)

我正在创建一个空目录,然后运行 firefox --new-instance --profile ./profile,然后手动将插件安装到配置文件中。但是这里有那个配置文件


心有法竹
浏览 187回答 1
1回答

皈依舞

在您的配置文件中有一个文件:prefs.js其中包含一行user_pref("extensions.lastAppBuildId", "20200707180101");对于禁用的插件,该行可能有问题。因此,您可以测试将此数字减 1 或删除整行(未经测试)。profile.set_preference("extensions.lastAppBuildId", "<apppID> -1 ")完整示例代码:from selenium.webdriver import FirefoxProfilefrom selenium import webdriverpath = '%APPDATA%\Mozilla\Firefox\Profiles\azk4wioue.default' #path to your profileprofile = FirefoxProfile(path)&nbsp;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')&nbsp;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 FirefoxProfilefrom selenium import webdriverprofile = webdriver.FirefoxProfile()profile.add_extension(extension='/Users/someuser/app/extension.xpi')driver = webdriver.Firefox(profile)如果插件在加载配置文件后存在但被禁用,您也可以尝试以下操作:def newTab(fx, url="about:blank"):&nbsp; &nbsp; wnd = fx.execute(selenium.webdriver.common.action_chains.Command.NEW_WINDOW)&nbsp; &nbsp; handle = wnd["value"]["handle"]&nbsp; &nbsp; fx.switch_to.window(handle)&nbsp; &nbsp; fx.get(url) # changes handle&nbsp; &nbsp; return fx.current_window_handledef retoggleAllTheAddons(fx):&nbsp; &nbsp; initialHandlesCount = len(fx.window_handles)&nbsp; &nbsp; addonsTabHandle = newTab(fx, "about:addons")&nbsp; &nbsp; fx.execute_script("""&nbsp; &nbsp; &nbsp; &nbsp; let hb = document.getElementById("html-view-browser");&nbsp; &nbsp; &nbsp; &nbsp; let al = hb.contentWindow.window.document.getElementsByTagName("addon-list")[0];&nbsp; &nbsp; &nbsp; &nbsp; let cards = al.getElementsByTagName("addon-card");&nbsp; &nbsp; &nbsp; &nbsp; for(let card of cards){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; card.addon.disable();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; card.addon.enable();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; """)&nbsp; &nbsp; if len(fx.window_handles) != 1:&nbsp; &nbsp; &nbsp; &nbsp; fx.switch_to.window(addonsTabHandle)&nbsp; &nbsp; &nbsp; &nbsp; fx.close()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python