Reputation: 203
I am new to using web driver but I have followed what was mentioned here (How can I launch Chrome with an unpacked extension?) and all that I could get from other web search.
I am trying to test an extension for chrome which I have developed but I haven't been able to figure out how to start chrome with extension loaded on it. Here is what I have till now and I would appreciate if someone could tell me the issue with the code (I was successful in launching Chrome using webdriver):
import time
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
browser = webdriver.Chrome() browser.get('http://seleniumhq.org/')
ChromeOptions options = new ChromeOptions();
options.addArguments("load-extension=C:\Users\mave\Desktop\Browser_Extension_Feature\extension_v5");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
time.sleep(15)
browser.quit()
Upvotes: 3
Views: 4024
Reputation: 57
This code should allow you to run an unpacked extension as desired, working as of September 2023.
It is worth mentioning, the path to the extension must seemingly be provided as an absolute path, and cannot be a path relative to the current working directory or anything similar.
In this example, it is assumed the extension is unpacked in a non-zipped folder.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
PATH_TO_CHROME_EXTENSION_DIR = r"C:\your\path\here"
options = Options()
options.add_extension(f"--load-extension={PATH_TO_CHROME_EXTENSION_DIR}")
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://www.google.com/')
input("press enter to end programme")
driver.quit()
Upvotes: 0
Reputation: 203
I was finally able to figure out how to run an unpacked extension and would leave this code for anyone who has similar troubles in future:
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("load-extension=C:\Users\mave\Desktop\Browser_Extension_Feature\extension_v5");
browser = webdriver.Chrome(chrome_options=chrome_options)
browser.get('http://www.seleniumhq.org/')
time.sleep(5)
browser.quit()
Upvotes: 2