0

I'm scraping some sites with selenium and proxies which need authentication so I use this extension to set proxy's credentials, at this point everything goes well.

def get_chromedriver(use_proxy=False, user_agent=None):
    path = os.path.abspath(
        os.path.join(os.path.dirname(__file__), './drivers/current/', 'chromedriver.exe'))
    chrome_options = webdriver.ChromeOptions()
    if use_proxy:
        pluginfile = 'proxy_auth_plugin.zip'

        with zipfile.ZipFile(pluginfile, 'w') as zp:
            zp.writestr("manifest.json", manifest_json)
            zp.writestr("background.js", background_js)
        chrome_options.add_extension(pluginfile)
    if user_agent:
        chrome_options.add_argument('--user-agent=%s' % user_agent)
    return driver

However, I need to set the following options too

  chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
    chrome_options.add_experimental_option('useAutomationExtension', False)
    chrome_options.add_argument("--disable-blink-features=AutomationControlled")
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--incognito")

The whole function looks like this, nevertheless when I execute the code below the proxy is not set, instead uses my public address. What's wrong with my code?

def get_chromedriver(use_proxy=False, user_agent=None):
    path = os.path.abspath(
        os.path.join(os.path.dirname(__file__), './drivers/current/', 'chromedriver.exe'))
    chrome_options = webdriver.ChromeOptions()
    if use_proxy:
        pluginfile = 'proxy_auth_plugin.zip'

        with zipfile.ZipFile(pluginfile, 'w') as zp:
            zp.writestr("manifest.json", manifest_json)
            zp.writestr("background.js", background_js)
        chrome_options.add_extension(pluginfile)
    if user_agent:
        chrome_options.add_argument('--user-agent=%s' % user_agent)
    chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
    chrome_options.add_experimental_option('useAutomationExtension', False)
    chrome_options.add_argument("--disable-blink-features=AutomationControlled")
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--incognito")
    driver = webdriver.Chrome(executable_path=path,
                              chrome_options=chrome_options)
    return driver
EDGAR MEDINA
  • 111
  • 1
  • 9
  • `chrome_options = webdriver.ChromeOptions()` should be `chrome_options = Options()` when imported from `from selenium.webdriver.chrome.options import Options` Check out the [documentation](https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.chrome.options) – It_is_Chris Dec 15 '21 at 21:07
  • @It_is_Chris I just tied it without success – EDGAR MEDINA Dec 15 '21 at 21:15
  • How are you calling the function? The params are set to `False` and `None` – It_is_Chris Dec 15 '21 at 21:17
  • Yeah, that's right. I just found that the incognito argument was disabling the extensions! – EDGAR MEDINA Dec 15 '21 at 21:34

1 Answers1

0

SOLVED After some debugging and trying one by one argument I discovered that the chrome_options.add_argument("--incognito") argument was blocking all the extensions in the browser. After being disabled it the program worked again. If someone else wants to add more extensions with the incognito argument should try this solution

EDGAR MEDINA
  • 111
  • 1
  • 9