Reputation: 1121
How can I redirect the traffic of Firefox launched by Selenium in Python to a proxy? I have used the solutions suggested on the web but they don't work!
I have tried:
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", "54.213.66.208")
profile.set_preference("network.proxy.http_port", 80)
profile.update_preferences()
driver = webdriver.Firefox(profile)
Upvotes: 18
Views: 56220
Reputation: 21
Here is the code for Selenium 4.x, to enable the proxy, you need to set browser options, these parameters from the bottom (ssl is https), and the rest is clear from the names, in order to find out these parameters, I went into the browser config and manually checked what to pick up.
I also know how to remove the bot check in the driver, for this you need to compile your driver on rast
def main():
url = "https://2ip.ru/"
options = Options()
options.set_preference("network.proxy.type", 1)
options.binary_location = r"C:\Program Files\Mozilla Firefox\firefox.exe"
options.set_preference("network.proxy.http", "xx.xxx.xxx.xxxx")
options.set_preference("network.proxy.http_port", 8000)
options.set_preference("network.proxy.ssl", "xx.xxx.xxx.xxx")
options.set_preference("network.proxy.ssl_port", 8000)
serviceDriver = Service(
executable_path=r"C:\Users\User\PycharmProjects\driver\geckodriver.exe")
driver = webdriver.Firefox(options=options, service=serviceDriver)
driver.get("https://2ip.ru")
Upvotes: 2
Reputation: 514
Try this for firefox/geckodriver:
proxy = "212.66.117.168:41258"
firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['proxy'] = {
"proxyType": "MANUAL",
"httpProxy": proxy,
"ftpProxy": proxy,
"sslProxy": proxy
}
driver = webdriver.Firefox(capabilities=firefox_capabilities)
And for Chrome you can use:
proxy = "212.66.117.168:41258"
prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = proxy
prox.socks_proxy = proxy
prox.ssl_proxy = proxy
capabilities = webdriver.DesiredCapabilities.CHROME
prox.add_to_capabilities(capabilities)
driver = webdriver.Chrome(desired_capabilities=capabilities)
Upvotes: 17
Reputation: 760
You need to import the following:
from selenium.webdriver.common.proxy import Proxy, ProxyType
Then setup the proxies:
myProxy = "xx.xx.xx.xx:xxxx"
proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': myProxy,
'ftpProxy': myProxy,
'sslProxy': myProxy,
'noProxy': '' # set this value as desired
})
Then call the webdriver.Firefox() function as follows:
driver = webdriver.Firefox(proxy=proxy)
driver.get("http://www.google.com")
Don't remember where exactly I found this solution, but its out there somewhere. Will surely provide a link if I find it again. Just took this part out of my code.
Upvotes: 22
Reputation: 2017
Your problem is on the driver init. Try webdriver = webdriver.Firefox(firefox_profile=profile)
All the other code is OK and you can also remove profile.update_preferences()
line.
I found YOUR solution with a 2 minute google search. How much time did you spend waiting for? :D
Your problem is that you maybe read code from other langs but Python. Replace this webdriver.Firefox(profile)
with webdriver.Firefox(firefox_profile=profile)
.
Your code should be:
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", "54.213.66.208")
profile.set_preference("network.proxy.http_port", 80)
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile)
Upvotes: 7