我需要使用扩展来测试Firefox。我想自动化测试并访问几个网站。
我安装了Selenium,它在geckodriver
打开。但是,扩展不存在。我可以从about:debugging
手动安装它,但问题是我希望Selenium测试启动gecko驱动程序,而扩展已经存在。这该怎么做?如何在geckodriver
中永久安装扩展,以便当我从硒中启动geckodriver
时它就在那里?
编辑:我还尝试从Firefox扩展网站安装扩展(将其添加到浏览器)。它被添加但是一旦我关闭壁虎窗口,扩展就会在下一次运行中消失。如何永久安装?
注意:OP没有指定语言,因此这个答案适用于Python。其他Selenium WebDriver语言绑定具有类似的机制,用于创建配置文件和添加扩展。
每次实例化驱动程序时都可以安装扩展。
首先,下载您想要的扩展名(XPI文件):https://addons.mozilla.org。
然后,在您的代码中...创建一个FirefoxProfile()
并使用add_extension()
方法添加扩展名。然后,您可以使用该配置文件实例化驱动程序。
例如,这将使用包含“HTTPS Everywhere”扩展名的新创建的配置文件启动Firefox:
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.add_extension(extension='https_everywhere-2019.1.31-an+fx.xpi')
driver = webdriver.Firefox(firefox_profile=profile)
您需要通过指定firefox的配置文件路径来启动具有现有配置文件的geckodriver
对于python,你可以这样做:
profile = FirefoxProfile('/home/student/.mozilla/firefox/gwi6uqpe.Default') // change this path
browser = webdriver.Firefox(firefox_profile=profile)
对于C#,你可以这样做:
string path = @"C:\Users\username\AppData\Local\Mozilla\Firefox\Profiles\myi5go1k.default";
FirefoxProfile ffprofile = new FirefoxProfile(path);
Driver = new FirefoxDriver(ffprofile);
您可以在特定的Firefox配置文件中永久安装扩展程序并使用它。为此,您需要按照以下步骤操作:
https://addons.mozilla.org/en-US/firefox/
package A_MozillaFirefox;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.ProfilesIni;
public class A_FirefoxProfile_dc_opt {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("FirefoxExtensionProfile");
FirefoxOptions opt = new FirefoxOptions();
opt.setProfile(testprofile);
WebDriver driver = new FirefoxDriver(opt);
driver.get("https://www.google.com");
}
}
浏览器快照: