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