How to run Selenium through proxy?

duriangray

Newbie
Joined
Jul 22, 2014
Messages
19
Reaction score
2
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 :)
 
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/')
 
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?
 
Back
Top