3

Hello I'm trying to install a Chrome extension with Selenium using python, I tried using ChromeDriver - WebDriver for Chrome

But it is not working, this is my code:

from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import ChromeOptions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.expected_conditions import presence_of_element_located

import re  # regular expressions, are imported from python directly
import time
import numpy as np
import pandas as pd
import functions_database

# Pandas read CSV
df_read = pd.read_csv(
    '/home/daniel/amazon-project-scrapers/ss_scraper.edited2.csv')

amazon_data = list(df_read.amz_search)

# Chrome Driver + install plugin
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/home/daniel/amazon-project-scrapers/chromedriver_linux64/DS-Amazon-Quick-View_v2.8.9.crx"));
ChromeDriver driver = new ChromeDriver(options);

driver = webdriver.Chrome(executable_path='/home/daniel/amazon-project-scrapers/chromedriver_linux64/chromedriver')
driver.get('https://www.amazon.com/')

And this is the error i'm getting:

File "camel_scraper.py", line 23
    ChromeOptions options = new ChromeOptions();
                        ^
SyntaxError: invalid syntax

I tried to do this in other 3 different ways, actually there is a similar question in Stack overflow whose answer is deprecated, if I find it again I'll write the link in here.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
The Dan
  • 1,408
  • 6
  • 16
  • 41
  • 1
    You're using the javascript syntax for setting the chromeoptions. You need to use the python syntax, as outlined on [this site](https://www.programcreek.com/python/example/100025/selenium.webdriver.ChromeOptions) – G. Anderson Jan 28 '20 at 18:54

1 Answers1

4

To add/install the DS-Amazon-Quick-View Chrome extension using Selenium's client you can use the following splution:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    chrome_options.add_extension('/home/daniel/amazon-project-scrapers/chromedriver_linux64/DS-Amazon-Quick-View_v2.8.9.crx')
    driver = webdriver.Chrome(options=chrome_options, executable_path='/path/to/chromedriver')
    driver.get('https://www.google.co.in')
    

Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • For some reason it gives me this issue: `selenium.common.exceptions.WebDriverException: Message: unknown error: cannot process extension #1 from unknown error: CRX verification failed: 3` – The Dan Jan 29 '20 at 09:07
  • @DaniAPred That seems to be an issue with the `foo.crx` extension file as it can't be processed by Selenium. From Selenium's perspective this code block is proven and robust. – undetected Selenium Jan 29 '20 at 09:11
  • 2
    This code works! I found the mistake, I'll put it here because I think this 2 following steps are very important for not getting the error I had. Download extension locally. (you can use online crx-downloader, for example https://crx-downloader.com/). (I USED ANOTHER DOWNLOADER, THIS ONE IS ACCEPTED BY GOOGLE, AND EXTENSIONS ARE UPDATED) Check version of your extension with site https://crx-checker.appspot.com/ (HERE YOU CAN VERIFY IF THE CRX IS REALLY UPDATED). If the CRX is CRX3, you will have no problems – The Dan Feb 03 '20 at 18:15