1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to run Selenium through proxy?

Discussion in 'General Programming Chat' started by duriangray, Sep 26, 2015.

  1. duriangray

    duriangray Newbie

    Joined:
    Jul 22, 2014
    Messages:
    5
    Likes Received:
    0
    Location:
    California
    I've been making a bot with selenium and python.

    How can I run this through proxies with authentication?

    1. I looked it up and some people are using browsermobproxy. Has anyone used this?
    2. Is it easier to connect all web traffic to the proxy with the server and then run my script? I'm not sure if this is possible?

    Any help appreciated :)
     
  2. MrBlue

    MrBlue Senior Member

    Joined:
    Dec 18, 2009
    Messages:
    974
    Likes Received:
    680
    Occupation:
    Web/Bot Developer
    If you are using Webdriver with Selenium and Python a simple solution would be to save the proxy's user/pass in your Firefox browser's profile and import said profile in your Python code.

    Example
    Code:
    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
    from selenium.webdriver.common.proxy import *
    
    profile = FirefoxProfile("/path/to/your/firefox/profile.default")
    
    
    myProxy = 'host:port'
    
    
    proxy = Proxy({
        'proxyType': ProxyType.MANUAL,
        'httpProxy': myProxy,
        'ftpProxy': myProxy,
        'sslProxy': myProxy,
        'noProxy': ''
        })
    
    
    browser = webdriver.Firefox(profile, proxy=proxy)
    browser.get('https://www.whatismyip.com/')
    
    
     
    • Thanks Thanks x 2
  3. rajanant

    rajanant Registered Member

    Joined:
    Sep 16, 2015
    Messages:
    62
    Likes Received:
    10
    Good answer, thanks!
    In case of several proxies should I create separate profile for each proxy and create new browser each time I want to switch to other one?
    Or there other way?