Reputation: 42357
I'm using Selenium WebDriver to test a Google Chrome extension I'm developing. I noticed that ChromeDriver
can be customised to add extensions to the instance of Chrome that it launches. This can be achieved using the AddExtension
and AddExtensions
methods of the ChromeOptions
class.
The documentation for these methods indicates that they require extensions to be provided as crx files. Since I'm developing the extension, I don't have a crx file. I would like to be able to load the unpacked extension, but I couldn't find a method to do this.
I tried putting the extension files in a zip file and specifying this for the AddExtension
method, but this caused an exception to occur since it wasn't a crx file. I also tried passing in the directory containing the unpacked files, but this produced a FileNotFoundException
.
How can I do this?
Upvotes: 16
Views: 14745
Reputation: 2052
Here is apython
example using webdriver_manager
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
# loading the extension Edit-This-Cookie
options.add_argument("--load-extension=./Edit-This-Cookie")
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=options)
driver.get("https://google.com")
Upvotes: 1
Reputation: 390
In Python3 it can be done like this:
from selenium.webdriver import Chrome, ChromeOptions
options = ChromeOptions()
options.add_argument("load-extension=/path/to/unpacked_ext")
driver = Chrome("/path/to/chromedriver", options=options)
# (optional) Look at the uploaded extension
driver.get("chrome://extensions")
Upvotes: 0
Reputation: 1327
For packed extensions (a .crx file)
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
For unpacked extensions (a local folder)
ChromeOptions options = new ChromeOptions();
options.addArguments("load-extension=/path/to/extension");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
Upvotes: 3
Reputation: 11
The unpacked extension error popped up for me and I requested for removing the restrictions in chrome which was enforced as organizational policy. Once the restrictions were removed, I am able to run the program with out any errors. ChromeBrowser-GPO-Deny - this was the one which was removed. You can check in Settings - Extensions - Check on Developer mode and see if the load unpacked extensions is checked once the restrictions are removed. You should be good then. All the above will work only when the chrome is not restricted.
Upvotes: 1
Reputation: 29669
The Selenium documentation has an example like this for a packed ( not unpacked) extension:
capabilities.setCapability("platform", "Windows 8" );
capabilities.setCapability("version", "10");
capabilities.setCapability("name", testname);
capabilities.setCapability("screen-resolution", "1280x1024");
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
if (isLocal) driver = new ChromeDriver(capabilities);
Upvotes: -1
Reputation: 321
It may be late but for future users:
https://sites.google.com/a/chromium.org/chromedriver/extensions
Upvotes: 2
Reputation: 42357
I was able to achieve this by using the AddArgument
method to directly pass the information to Chrome. Here's what it looks like in C#:
options = new ChromeOptions();
options.AddArgument("--load-extension=" + unpackedExtensionPath);
Upvotes: 20