How to use authenticated proxy in selenium chromedriver?

前端 未结 7 855
挽巷
挽巷 2020-12-14 08:10

After searching for many hours I am starting to think this is impossible.

I need to run Chrome through selenium using different authenticated (not public) proxy\'s f

相关标签:
7条回答
  • 2020-12-14 08:40

    I could not find any solution for chrome. We can not add extensions with headless option. I am using Heroku with chrome-buildpack. There are following options

    1. Use xvfb instead of headless options and install extension
    2. Use local proxy forwarder that forwards traffic to authenticated proxy; we can use Squid, mitProxy, or something like proxy-login-automator

    Instead of these workaround I switched to Firefox where i was able to fill Username and Password on Proxy authentication Pop-up. Like given below. Following code is for Ruby using Capybara. You should be able to do something like this on your platform

    
    page.driver.browser.switch_to.alert.send_keys('proxy-username' + Selenium::WebDriver::Keys::KEYS[:tab] + 'my-password')
    page.driver.browser.switch_to.alert.accept
    
    0 讨论(0)
  • 2020-12-14 08:41

    This is the best solution I found and is the ONLY one that worked - all other answers on this question are outdated. It basically generates an auth extension for Chrome on the fly. Simply use the function as defined in the script as follows:

    driver = proxy_chrome(host, port, username, password)
    driver.get("http://www.icanhazip.com/")
    driver.get("https://www.reddit.com/")
    print('Terminated without issues.')
    

    Note that this doesn't work with the --headless option. However, on Linux, you can simply use the x virtual frame buffer to simulate that. It's as easy as this in python:

    import xvfbwrapper
    x = xvfbwrapper.Xvfb()
    x.start()
    
    0 讨论(0)
  • 2020-12-14 08:50

    after trying many solutions that didn't actually work properly, i finally managed to set the authenticated proxy using the suggested extension from previous answers. what you need to do is to enter this link:

    http://crxextractor.com/ and paste this url: https://www.crx4chrome.com/crx/1446/

    It will let you download the extention as a .crx file without installing it. than i used this code:

    proxy = {'address': 'pr.oxylabs.io:7777',
         'username': 'USERNAME',
         'password': 'PASSWORD'}
    
    capabilities = dict(DesiredCapabilities.CHROME)
    capabilities['proxy'] = {'proxyType': 'MANUAL',
                             'httpProxy': proxy['address'],
                             'ftpProxy': proxy['address'],
                             'sslProxy': proxy['address'],
                             'noProxy': '',
                             'class': "org.openqa.selenium.Proxy",
                             'autodetect': False,
                             'socksUsername': proxy['username'],
                             'socksPassword': proxy['password']}
    
    options = webdriver.ChromeOptions()
    options.add_extension("./extension_2_0_0_0.crx")
    driver = webdriver.Chrome(executable_path=CHROME_PATH, desired_capabilities=capabilities, chrome_options=options)
    
    0 讨论(0)
  • 2020-12-14 08:51

    THis is a temporary solution might work in initial state: Code is in Python: Download the plugin first from chrome plugin store : Proxy-Auto-Auth_v2.0.crx

            options = webdriver.ChromeOptions()
            options.add_extension("./Proxy-Auto-Auth_v2.0.crx")) #this will provide you a window to enter user name and proxy 
            driver = webdriver.Remote(command_executor=selenium_server,desired_capabilities=options.to_capabilities())
    
            or 
    
            driver = webdriver.Chrome(chrome_options=options)
    
    0 讨论(0)
  • 2020-12-14 08:59

    I have checked for most of the solutions on the web and for none of them authentication via chrome/firefox desired capabilities is working. Check this link: https://github.com/webdriverio/webdriverio/issues/324. Finally the temporary solution is to whitelist your IP address with the proxy provider.

    0 讨论(0)
  • 2020-12-14 09:02

    To use proxies with auth in python selenium you can use seleniumwire.

    Fistly, install it with pip install selenium-wire

    Then import webdriver from seleniumwire instead selenium

    from seleniumwire import webdriver
    options = {
        'proxy': {
            'http': 'http://username:password@host:port', 
            'https': 'https://username:password@host:port',
            'no_proxy': 'localhost,127.0.0.1' # excludes
        }
    }
    browser = webdriver.Chrome(path_to_driver, seleniumwire_options=options)
    

    Now you can use your browser instance exact the same way as selenium: browser.get('https://api.ipify.org') and so on...

    0 讨论(0)
提交回复
热议问题