Selenium WebDriver 3 Practical Guide
上QQ阅读APP看书,第一时间看更新

Adding ChromeExtensions

Similar to Firefox, we can add extensions to the Chrome browser by specifying the location of the extension. We can add Packed (.crx file) or Unpacked (folder) extensions using the ChromeOptions class. 

To add a Packed extension, we need to call the addExtension() method:

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addExtensions(new File("/path/to/extension.crx"));
ChromeDriver driver = new ChromeDriver(chromeOptions);

To add an Unpacked extension, we need to use the addArguments() method, which will load the extension reading the specified folder while launching the Chrome binary. This is done as follows:

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("load-extension=/path/to/extension");
ChromeDriver driver = new ChromeDriver(chromeOptions);

Similarly, you can use Chrome options to add more extensions, arguments, and Binaries to your Chrome browser.