python-3.x Selenium Grid 4 for Firefox缺少扩展插件选项吗?

a9wyjsp7  于 2022-12-20  发布在  Python
关注(0)|答案(2)|浏览(98)

我让Selenium在本地工作得很好,使用下面的设置添加扩展。

from selenium import webdriver
from selenium.webdriver.firefox.options import Options as firefox_options

def init_firefox(self,threadname,headless,images_off):

        if headless == True:
            options=firefox_options()
            options.add_argument("--headless")
            driver = webdriver.Firefox(options=options,executable_path=(r"C:\Users\charl\OneDrive\python\gecko\geckodriver.exe"))
        else:
            options=firefox_options()
            driver = webdriver.Firefox(executable_path=(r"C:\Users\charl\OneDrive\python\gecko\geckodriver.exe"))

        extension_dir = ('C:\\Users\\charl\\OneDrive\\python\\gecko\\extensions\\')
        extensions = [
            'firefox@vid.io.xpi',
            'noimages.xpi',
            ]
        
        for extension in extensions:
            driver.install_addon(extension_dir + extension, temporary=True)
        self.close_tab(driver)        
        self.login(driver)
        return driver

但当我尝试使用以下代码在Selenium Grid 4上执行相同的操作时:-

from selenium import webdriver
from selenium.webdriver.firefox.options import Options as firefox_options

def init_firefox_remote(self,threadname,headless,images_off):

        if headless == True:
            #options = Options()
            options=firefox_options()
            options.add_argument("--headless")
            driver = webdriver.Remote(command_executor='http://xx.xxx.xx.xx:4444/wd/hub', options=options)
        else:
            options=firefox_options()
            driver = webdriver.Remote(command_executor='http://xx.xx.xx.xx:4444/wd/hub', options=options)

        extension_dir = ('/dev/shm/extensons/')
        extensions = [
            'firefox@vid.io.xpi',
            'noimages.xpi',
            ]
        
        for extension in extensions:
            driver.install_addon(extension_dir + extension, temporary=True)
        self.close_tab(driver)        
        self.login(driver)
        return driver

我得到一个错误:-

AttributeError: 'WebDriver' object has no attribute 'install_addon'

我使用的Selenium Grid版本是这样创建的

$ docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-firefox:4.0.0-beta-3-prerelease-20210321

有什么想法吗?Selenium Grid for Firefox缺少安装扩展选项吗?

f8rj6qna

f8rj6qna1#

在远程Firefox浏览器中安装插件是通过创建Firefox配置文件并在其中添加扩展来完成的:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options as FirefoxOptions

profile = webdriver.FirefoxProfile()
options = FirefoxOptions()

profile.add_extension("/path/to/extension.xpi")

driver = webdriver.Remote(command_executor="http://xx.xx.xx.xx:4444/wd/hub",
                          options=options,
                          browser_profile=profile)

我不认为在远程火狐浏览器中加载临时插件是可能的。

yi0zb3m4

yi0zb3m42#

install_addon仅适用于本地Web驱动程序。使用远程Web驱动程序时需要一个简单的解决方案,如this issue中所述。
更具体地说,更改此行:

driver.install_addon(extension_dir + extension, temporary=True)

webdriver.Firefox.install_addon(driver, extension_dir + extension, temporary=True)

完整代码应如下所示:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options as firefox_options

def init_firefox_remote(self,threadname,headless,images_off):

        if headless == True:
            #options = Options()
            options=firefox_options()
            options.add_argument("--headless")
            driver = webdriver.Remote(command_executor='http://xx.xxx.xx.xx:4444/wd/hub', options=options)
        else:
            options=firefox_options()
            driver = webdriver.Remote(command_executor='http://xx.xx.xx.xx:4444/wd/hub', options=options)

        extension_dir = ('/dev/shm/extensons/')
        extensions = [
            'firefox@vid.io.xpi',
            'noimages.xpi',
            ]
        
        for extension in extensions:
            webdriver.Firefox.install_addon(driver, extension_dir + extension, temporary=True)
        self.close_tab(driver)        
        self.login(driver)
        return driver

我已经在Selenium文档中打开了a pull request,以阐明此类用法。

相关问题