亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 selenium 下啟動的瀏覽器不使用插件

在 selenium 下啟動的瀏覽器不使用插件

心有法竹 2023-04-18 15:43:01
我正在使用 python+selenium,配置文件有插件。在啟動時,它會暫時顯示它們,但隨后它們會被隱藏起來。它們存在,它們沒有被禁用,但它們是隱形的并且不起作用。我可以禁用和啟用它,然后它會出現在任務欄上并且可以正常工作。使用配置文件手動調用 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: 2147746065JavaScript error: resource://gre/modules/ExtensionContent.jsm, line 554: TypeError: Argument 1 of PrecompiledScript.executeInGlobal is not an object.1596129672037   Marionette  INFO    Listening on port 412851596129672136   Marionette  WARN    TLS certificate errors will be ignored for this sessionJavaScript error: undefined, line 14: Error: An unexpected error occurredJavaScript 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 pythonfrom selenium.webdriver import Firefoxfrom selenium.webdriver.firefox.options import Optionsprofile_path='./profile'opts=Options()opts.profile=profile_pathdriver=Firefox(options=opts)breakpoint()版本可能更重要:火狐瀏覽器 68.9.0esr壁虎驅動程序 0.26.0 (e9783a644016 2019-10-10 13:38 +0000)我正在創建一個空目錄,然后運行 firefox --new-instance --profile ./profile,然后手動將插件安裝到配置文件中。但是這里有那個配置文件
查看完整描述

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()


查看完整回答
反對 回復 2023-04-18
  • 1 回答
  • 0 關注
  • 222 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號