答案是 肯定的 ,Selenium 2支持(远程)安装浏览器扩展。
Chrome和Firefox WebDriver支持远程安装扩展。以下是Chrome和Firefox的示例代码:
Chrome
File file = new File("extension.crx"); // zip files are also acceptedChromeOptions options = new ChromeOptions();options.addExtensions(file);// Option 1: Locally.WebDriver driver = new ChromeDriver(options);// Option 2: RemotelyDesiredCapabilities capabilities = DesiredCapabilities.chrome();capabilities.setCapability(ChromeOptions.CAPABILITY, options);WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);Firefox
File file = new File("extension.xpi");FirefoxProfile firefoxProfile = new FirefoxProfile();firefoxProfile.addExtension(file);// Option 1: LocallyWebDriver driver = new FirefoxDriver(firefoxProfile);// Option 2: RemotelyDesiredCapabilities capabilities = DesiredCapabilities.firefox();capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);我还实现了Opera和Safari扩展程序的自动安装,它们已在上游合并:
- OperaDriver:https : //github.com/operasoftware/operadriver/pull/93
- SafariDriver:https://github.com/SeleniumHQ/selenium/pull/87
歌剧
此API与FirefoxDriver类似。
File file = new File("extension.oex"); // Must end with ".oex"OperaProfile operaProfile = new OperaProfile();operaProfile.addExtension(file);// Option 1: LocallyWebDriver driver = new OperaDriver(operaProfile);// Option 2: RemotelyDesiredCapabilities capabilities = DesiredCapabilities.opera();capabilities.setCapability("opera.profile", operaProfile);WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);苹果浏览器
此API与ChromeDriver类似。
File file = new File("extension.safariextz");SafariOptions options = new SafariOptions();options.addExtensions(file);// Option 1: Locally.WebDriver driver = new SafariDriver(options);// Option 2: RemotelyDesiredCapabilities capabilities = DesiredCapabilities.safari();capabilities.setCapability(SafariOptions.CAPABILITY, options);WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);IE浏览器
祝好运。



