How to load extension within chrome driver in selenium with python
Asked Answered
R

6

4

All my efforts to open chrome browser with Browsec extension enabled are failing. Here is what i tried in last -

# Configure the necessary command-line option.
options = webdriver.ChromeOptions()
options.add_argument(r'--load- 
extension=C:\Users\lap0042\AppData\Local\Google\Chrome\User 
Data\Default\Extensions\omghfjlpggmjjaagoclmmobgdodcjboh')

# Initalize the driver with the appropriate options.
driver = webdriver.Chrome(chrome_options=options)

driver.get("http://stackoverflow.com")

This results in error "Failed to load extension from . Manifest files is missing or unreadable"

After search for this error I get that Manifest.json file should be renamed to manifest.json.txt but doing this resulted in same error.

Any help will be highly appreciated

enter image description here

Recovery answered 21/5, 2018 at 7:30 Comment(0)
S
6

To open chrome browser with any extension you need to use the add_extension() method through an instance of chrome.options class and you can use the following solution :

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_extension(r'C:\path\to\extension.crx')
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://www.google.co.in')
print("Page Title is : %s" %driver.title)
driver.quit()

References

You can find the relevant documentation in:

You can find a couple of relevant discussions in:

Switchboard answered 21/5, 2018 at 10:14 Comment(3)
Thanks DebanjanB, this indeed add extension in the chrome but is disabled. is there any any that browser open with enabled extension? or what would be the best way to enable extension so that this may be used.Recovery
@Recovery What exactly do you men by but is disabled, check the documentation I have added within my answer for your reference.Switchboard
I mean extension is not activated. I want that when chromedriver is launched, it launches with extension already activatedRecovery
M
1

if i understood correctly you're trying to load a local unpacked extension into selenium

in that case this code should work

options = options()
options.add_argument("--load-extension=" + unpackedExtensionPath)

a better option would be to pack your extension into a crx file

Mcginty answered 28/5, 2022 at 23:23 Comment(0)
L
0

Use this code to fetch extensions

from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/pathtoChromeextension.crx")); //adding 
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);

Use below to get crx file http://crxextractor.com/ from your extension id which is omghfjlpggmjjaagoclmmobgdodcjboh

Lepidopterous answered 21/5, 2018 at 7:37 Comment(5)
Thanks Prany for your help. Im trying to find the CRX of Browsec or any good VPN plugin but no success yet. The app on the link crxextractor.com doesn't show any crx when i try to add file with extension id in it. In fact, there is no .CRX file in the folder of extension id. Can you help how i can find crx of any good VPN pluginRecovery
OK i extracted .crx file. Now can you please confirm if above code is for python 3? as it is showing errorRecovery
@Recovery - Have you added this at the top - 'from selenium.webdriver.chrome.options import Options' and 'from selenium.webdriver.common.desired_capabilities import DesiredCapabilities'Lepidopterous
I think the script you shared is for java, Im looking for PyhtonRecovery
Prany. This is not correct syntax for python 3 as i get a lot of syntax errors on running this codeRecovery
B
0

Simplest answer as far as I'm aware - manifest in subfolder of location you've referenced (e.g. 3.28.2_0' or whatever the latest version of extension is...)

This assumes you're using 'options.add_argument('--load-extension=')...

For options.add_extension('reference crx file .crx')

Brunhild answered 16/2, 2021 at 1:38 Comment(2)
possible duplicate of https://mcmap.net/q/66321/-load-chrome-extension-using-seleniumBrunhild
(In actuality, the correct answer is what I stated above - all this person needs to do is include the sub-folder name the extension path)....Brunhild
R
0

May be I am late to the party but this may help other. None of solution above worked for me. I am using different chrome-binary path setting in my python selenium code.

Solution:

  1. Go to extensions and check the extension ID (this will be the extension dir)

  2. Now inside your chrome dir locate the "Profile" which has the extension.

  3. Go to the extensions dir and copy the folder with name equal to our Extension ID

  4. Paste the dir inside chrome-win64 binary folder (i am using windows).

  5. Add this piece of code (eg. ID):

     # Add the path to your extension
     epath = r"dknlfmjaanfblgfdfebhijalfmhmjjjo\0.4.8_0"
     options.add_argument(f"--load-extension={epath}")
    
Retroversion answered 23/5 at 22:19 Comment(0)
A
-1

For Python you need wright path to manifest.json file

from selenium.webdriver.chrome.options import Options
from selenium import webdriver

path = os.path.dirname(r"C:\temp\mdnleldcmiljblolnjhpnblkcekpdkpa\19.5.1.10_0\manifest.json")

options = Options()
options.add_argument(f"--load-extension={path}")
driver = webdriver.Chrome(options=options)

Aaronaaronic answered 23/5, 2019 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.