How to load extension within chrome driver in selenium with python - python

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

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:
ChromeDriver - WebDriver for Chrome.
You can find a couple of relevant discussions in:
[Python] How to install Chrome Extension using Selenium & Python
[Java] How to install extension permanently in geckodriver

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

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')

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

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)

Related

Cannot add extension crx file chrome

I can't add extensions crx file. Chrome always open but no extensions. did i do wrong somewhere?
one more thing i use undetected-chromedriver
https://github.com/ultrafunkamsterdam/undetected-chromedriver
my code:
import undetected_chromedriver as uc
...
options = uc.ChromeOptions()
options.add_extension('./capcha.crx')
options.add_extension('./metamask.crx')
driver = uc.Chrome(use_subprocess=True,options = options,user_data_dir=path_profile_chrome)
You can try this code
options.add_argument(
"--load-extension=path\\to\\extension1,path\\to\\extension2")

Python - Selenium Storing Extensions' data

I using metamask extension but when i restart the code, extension data is deleting. How can i setup that. I don't want to do that more times. My now code is like: `
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as coptions
PATH = './chromedriver.exe'
options = coptions()
options.add_extension("meta.crx")
driver = webdriver.Chrome(PATH, options=options)
driver.get("https://www.google.com")
input("enter?")
driver.quit()
`
How can i do that ?

Chrome Webdriver doesn't load the default profile

The Selenium Chrome Webdriver does not load my default Chrome profile.
I already tried many other Stack Overflow solutions including changing the path (and using the local Chrome App, which gives a 'permission denied' Error).
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=~/Library/Application Support/Google/Chrome/Default")
options.add_argument("--profile-directory=Default")
driver = webdriver.Chrome()
driver.get("https://tagmanager.google.com/#/home")
assert "Google Analytics" in driver.title
account_element = driver.find_element_by_css_selector("div.account.card div#149385038 table")
accountPublicId = driver.find_element_by_css_selector("div#149385038 table td.account__public-id")
The result is still the same; it loads only a 'naked' Chrome Webdriver instead of loading the local default profile (I use everyday for work).
Update:
I don't know how or why but now, when I quit Chrome and start Chrome through the Python Script, Google Chrome starts with my profile but does not use the cookies I have in that profile.
I will see if I can add the cookies "manually" with an options.add_arguments.
Try this:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\ProfilePath") ##profile path
w = webdriver.Chrome(executable_path="C:\\chromedriver.exe", chrome_options=options)
you may try this
options.add_argument("user-data-dir=C:\\Test")
did not add anything to the
options arguments (looking via debugger it was an empty list)
options.arguments.append("user-data-dir=C:/test/") managed to update the list of arguments
and it worked.

Error while installing chrome extension through Python - selenium

I am trying to install chrome extension using Python Selenium.
When I click Add to chrome button a pop up(don't know whether it is a java scripted) is generated asking: "Add extension", "Cancel". I want to click "Add extension" but I am getting following error:
selenium.common.exceptions.NoAlertPresentException: Message: no alert open
My code:
from selenium import webdriver
import time
driver=webdriver.Chrome()
driver.implicitly_wait(30)
driver.get("https://chrome.google.com/webstore/detail/buyhatke/jaehkpjddfdgiiefcnhahapilbejohhj?hl=en")
time.sleep(15)
element=driver.find_element_by_css_selector("body > div.F-ia-k.S-ph.S-Rc-qa > div.h-F-f-k.F-f-k > div > div > div.e-f-o > div.h-e-f-Ra-c.e-f-oh-Md-zb-k >
div.dd-Va.g-c-wb.g-eg-ua-Uc-c-za.g-c-Oc-td-jb-oa.g-c")
element.click()
alert = driver.switch_to.alert
alert.accept()
help me to install it.
Updated code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import os
executable_path = "C:\\Users\\SACHIN\\AppData\\Local\\Programs\\Python\\Python36\\chromedriver"
os.environ["webdriver.chrome.driver"] = executable_path
chrome_options = Options()
chrome_options.add_extension("C:\\Users\\SACHIN\\AppData\\Local\\Google\\chrome\\User Data\\Default\\Extensions\\jaehkpjddfdgiiefcnhahapilbejohhj\\
3.4.143_0")
driver = webdriver.Chrome(executable_path=executable_path,chrome_options=chrome_options)
driver.get("http://stackoverflow.com")
driver.quit()
This is because the download option pop up that you are trying to select using switch_to.alert is not actually a JS alert pop up. You can not select is using Selenium's switch_to methods. You need to use the DesiredCapabilities class to select this option and then use it in your code.
As per the ChromeDriver documentation, given here, please use the packed( the one with .crx extension ) or unpacked extension file (directory containing the extension, including a manifest.json file) and load it using DesiredCapabilities. This can be done using
from selenium.webdriver.chrome.options import Options
executable_path = "path_to_webdriver"
os.environ["webdriver.chrome.driver"] = executable_path
chrome_options = Options()
chrome_options.add_extension('path_to_extension')
driver = webdriver.Chrome(executable_path=executable_path,chrome_options=chrome_options)
driver.get("http://stackoverflow.com")
driver.quit()

Getting Chrome to launch via Selenium

I am having issues getting an instance of a Chrome browser from selenium in python. I'm using Windows 8. I have downloaded the chromedriver binary and added it to my path but I get the following error in Python:
selenium.common.exceptions.WebDriverException: Message: 'ChromeDriver executable needs to be available in the path.
This error occurs for the following line:
driver = webdriver.Chrome(executable_path='path\to\chromedriver_win32_2.0')
Two ways to set it, you somehow mixed up.
Put the chromedriver.exe's path into PATH (on Windows), so your PATH setting is correct, but you need to call the default constructor.
driver = webdriver.Chrome()
Specify the path in webdriver.Chrome(executable_path='some path'). Here you need the full path to the executable, not the directory.
webdriver.Chrome(executable_path=r'C:\Users\HaranKumar\Downloads\chromedriver_win32_2.0\chromedriver.exe')
Choose either one you want.
Assuming that your path is correct, make sure that you include the chromedriver itself: chromedriver.exe
I used the following and it worked! Thanks!
driver = webdriver.Chrome(executable_path=r'C:\chromedriver.exe')
#put your own path between the ''
Even if you have chromedriver.exe in the PATH, its necessary to have chromedriver.exe in the folder where your executable scripts are present(atleast so is the case when it comes to python scripts)
for python(selenium) you will need:
from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
then put your chromedriver.exe path in. the "r" is just to prevent it from detecting the \ and causing errors in python
PATH = r"C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
options = webdriver.ChromeOptions()
you can now order the driver to get websites
driver.get('http://www.google.com')
Update 2021
For Python
I got Selenium to open my default Chrome browser with my profile using this:
options.add_argument("--user-data-dir=C:\\Users\\Sams\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument('--profile-directory=Default')
Go to: chrome://version/
Look for your "Profile Path"
profile path image
Copy your Profile Path and replace "options.add_argument("--user-data-dir={YOUR PROFILE PATH}")"
The second argument may also look different. Mine so happens to be "Default". For other it may be "Profile 1" or "Profile X" X being an incrementing number.
Close all your Chrome browsers before running. Because the Chrome Driver cannot run an automated browser alongside the rest of your other tabs.
Here is my entire Selenium Config. Hopefully it helps someone.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.remote.webdriver import WebDriver
options = Options()
options.add_argument("--window-size=1920,1080")
options.add_argument("--log-level=3")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
# The 2 arguments below will use your main browser.
options.add_argument("--user-data-dir=C:\\Users\\Sams\\AppData\\Local\\Google\\Chrome\\User Data") # profile path (C)
options.add_argument('--profile-directory=Default')
options.headless = False # To show Chrome or not to show?
PATH = (r"C:\Users\Sams\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\selenium\webdriver\chrome\chromedriver.exe")
CHROMEDRIVER_PATH = PATH
driver = webdriver.Chrome(CHROMEDRIVER_PATH, options=options)
I recently ran into this error and I found three main solutions for this chromedriver not in path error
download a library that does it - webdriver_manager or chromedriver_autoinstaller both work
add it to path in your program
add chromedriver executable to your system PATH
Update 2016
The following solution works for me, with WebDriver 3.0.1, Chrome Driver 2.25.426923, Window 7
System.setProperty("webdriver.chrome.driver","D:\\workspace\\chromedriver.exe");
WebDriver driver;
driver = new ChromeDriver();
*Note:
Chrome Driver
See also: http://www.frontendtest.org/blog/path-executable-chrome/

Categories

Resources