Running Selenium Webdriver with a proxy in Python not changing IP - python

I am trying to make a proxy in python using selenium i have a list of free proxies from https://free-proxy-list.net/ but the ip i get is always the same, there is something wrong with this setup?
PROXY = proxy_list[11]
options = Options()
options.headless = False
webdriver.DesiredCapabilities.FIREFOX['proxy'] = {
"httpProxy":PROXY,
"ftpProxy":PROXY,
"sslProxy":PROXY,
"noProxy":None,
"proxyType":"MANUAL",
"class":"org.openqa.selenium.Proxy",
"autodetect":False
}
driver = webdriver.Firefox(options=options, executable_path=r'geckodriver.exe')
# Set the interceptor on the driver
driver.request_interceptor = interceptor
driver.get(url)
time.sleep(5)
driver.quit()
EDIT: i am running according to the response the following code:
url = 'https://www.whatismyip.com/es/'
PROXY = proxy_list[6]
options = Options()
options.headless = False
firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['proxy'] = {
'proxyType': "MANUAL",
'httpProxy': PROXY,
'ftpProxy': PROXY,
'sslProxy': PROXY
}
driver = webdriver.Firefox(options=options, executable_path=r'geckodriver.exe', capabilities = firefox_capabilities)
# Set the interceptor on the driver
driver.request_interceptor = interceptor
driver.get(url)
time.sleep(50)
driver.quit()
But the ip in the webpage is always the same, dunno if missunderstood the use of the proxy or i am doing something wrong

If you use Firefox browser,
...
# set proxy
firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['proxy'] = {
'proxyType': "MANUAL",
'httpProxy': PROXY,
'ftpProxy': PROXY,
'sslProxy': PROXY
}
driver = webdriver.Firefox(options=options, executable_path=r'geckodriver.exe', capabilities = firefox_capabilities)
If you use Chrome,
...
chrome_options = Options()
chrome_options.add_argument('--proxy-server=' + PROXY)
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(executable_path = 'chromedriver.exe', options=chrome_options)

Related

Proxy in Selenium Python with Firefox

This is the code that I'm using, I've tried numerous other solutions from stackoverflow, but everytime I make a request to httpbin.org/ip I see my own IP.
# Selenium Logging
selenium_logger = logging.getLogger('seleniumwire')
selenium_logger.setLevel(logging.ERROR)
# Web driver settings
# Profile
profile = webdriver.FirefoxProfile()
profile.accept_untrusted_certs = True
# Capabilities
firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
# Options
options = Options()
options.headless = False
brightdata_proxy = "personal_brighdata_url"
firefox_proxies = Proxy()
firefox_proxies.ssl_proxy = brightdata_proxy
firefox_proxies.add_to_capabilities(firefox_capabilities)
self._driver = webdriver.Firefox(executable_path=GeckoDriverManager().install(),
firefox_profile=profile,
capabilities=firefox_capabilities,
options=options,
)
Can anybody help me to figure those proxies out ?

DeprecationWarning: capabilities and desired_capabilities have been deprecated, please pass in a Service object

from selenium import webdriver
firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
proxy = "115.77.166.65:41710"
firefox_capabilities['proxy'] = {
"proxyType": "MANUAL",
"httpProxy": proxy,
"ftpProxy": proxy,
"sslProxy": proxy
}
driver = webdriver.Firefox(capabilities=firefox_capabilities)
driver.get("https://whoer.net/")
driver = webdriver.Firefox(capabilities=firefox_capabilities)
DeprecationWarning: capabilities and desired_capabilities have been deprecated, please pass in a Service object
How to fix?

open chrome tab with proxy using selenium python

capabilities = webdriver.DesiredCapabilities.CHROME
Proxy.add_to_capabilities(capabilities)
driver = webdriver.chrome('tmp/chromedriver', desired_capabilities=capabilities)
driver.get("https://www.google.com/")`
I'm trying to open a chrome tab with proxy (no authentication needed) but I got this error:
add_to_capabilities() missing 1 required positional argument: 'capabilities' error
Try following code as EX:
from selenium import webdriver
from selenium.webdriver.common.proxy import *
proxy_url = "127.0.0.1:9009"
proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': proxy_url,
'sslProxy': proxy_url,
'noProxy': ''})
capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)
driver = webdriver.Chrome(desired_capabilities=capabilities)
driver.get("http://example.org")

How can I set socks5 proxy for selenium webdriver? Python

I really can’t to set socks5 proxy(http too...) for my chrome webdriver in selenium for python.
I tried many different ways... But I think I do something bad.
Example 1:
self.options.add_argument('--proxy-server=http://'+proxy)
Example 2:
webdriver.DesiredCapabilities.CHROME['proxy'] = {
"socksProxy": proxy,
"ftpProxy": proxy,
"sslProxy": proxy,
"noProxy": None,
"proxyType": "MANUAL",
"class": "org.openqa.selenium.Proxy",
"autodetect": False
}
Please describe fully the working example of setting up socks5 proxy on Selenium for Python and Chrome webdriver, with an example of proxy string formats (maybe i am doing something mistakes here ...).
PS Two problems which I get:
Just staying old IP address.
No internet connection in chrome web driver.
Chrome do not allow proxy with auth. I am not shure but after read so many informations I think so.... Only one way is working for me - to use proxy socks5 without auth by login and password.
options = webdriver.ChromeOptions()
proxy = '12.12.421.125:1949'
options.add_argument('--proxy-server=socks5://' + proxy)
driver = webdriver.Chrome(options=options)
For FireFox's geckodriver if you just want to set socks5 host / socks5 proxy :-
form selenium import webdriver
profile = webdriver.FirefoxProfile()
# Socks5 Host SetUp:-
myProxy = "198.199.101.152:8388"
ip, port = myProxy.split(':')
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', ip)
profile.set_preference('network.proxy.socks_port', int(port))
driver = webdriver.Firefox(firefox_profile=profile)
Here is the code I used to connect to a Socks5 server with username/password auth.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
capabilities = dict(DesiredCapabilities.CHROME)
capabilities['proxy'] = {
'proxyType': 'MANUAL',
'socksProxy': '<Your_IP>:<Your_Port>',
'socksVersion': 5,
'ftpProxy': '<Your_IP>:<Your_Port>',
'noProxy': 'localhost,127.0.0.1',
'class': "org.openqa.selenium.Proxy",
'autodetect': False
}
capabilities['proxy']['socksUsername'] = '<username>'
capabilities['proxy']['socksPassword'] = '<password>'
driver = Chrome(ChromeDriverManager().install(), desired_capabilities=capabilities)
For Firefox's geckodriver , based on the answer by Tanmay Harsh, I have got this so far:
from selenium.webdriver import Firefox, FirefoxOptions
proxy = ('proxy-server.local', 1080)
options = FirefoxOptions()
options.set_preference('network.proxy.type', 1)
options.set_preference('network.proxy.socks', proxy[0])
options.set_preference('network.proxy.socks_port', proxy[1])
options.set_preference('network.proxy.socks_remote_dns', True)
driver = Firefox(options=options)
'proxy': {
'http': 'socks5://user:pass#192.168.10.100:8888',
'https': 'socks5://user:pass#192.168.10.100:8888',
'no_proxy': 'localhost,127.0.0.1'
}
}
driver = webdriver.Chrome(seleniumwire_options=options)```
source - https://github.com/wkeeling/selenium-wire#socks

How do I set proxy for chrome in python webdriver?

I'm using this code:
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", "proxy.server.address")
profile.set_preference("network.proxy.http_port", "port_number")
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile)
to set proxy for FF in python webdriver. This works for FF. How to set proxy like this in Chrome? I found this exmaple but is not very helpful. When I run the script nothing happens (Chrome browser is not started).
from selenium import webdriver
PROXY = "23.23.23.23:3128" # IP:PORT or HOST:PORT
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)
chrome = webdriver.Chrome(options=chrome_options)
chrome.get("http://whatismyipaddress.com")
Its working for me...
from selenium import webdriver
PROXY = "23.23.23.23:3128" # IP:PORT or HOST:PORT
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=http://%s' % PROXY)
chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://whatismyipaddress.com")
I had an issue with the same thing. ChromeOptions is very weird because it's not integrated with the desiredcapabilities like you would think. I forget the exact details, but basically ChromeOptions will reset to default certain values based on whether you did or did not pass a desired capabilities dict.
I did the following monkey-patch that allows me to specify my own dict without worrying about the complications of ChromeOptions
change the following code in /selenium/webdriver/chrome/webdriver.py:
def __init__(self, executable_path="chromedriver", port=0,
chrome_options=None, service_args=None,
desired_capabilities=None, service_log_path=None, skip_capabilities_update=False):
"""
Creates a new instance of the chrome driver.
Starts the service and then creates new instance of chrome driver.
:Args:
- executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH
- port - port you would like the service to run, if left as 0, a free port will be found.
- desired_capabilities: Dictionary object with non-browser specific
capabilities only, such as "proxy" or "loggingPref".
- chrome_options: this takes an instance of ChromeOptions
"""
if chrome_options is None:
options = Options()
else:
options = chrome_options
if skip_capabilities_update:
pass
elif desired_capabilities is not None:
desired_capabilities.update(options.to_capabilities())
else:
desired_capabilities = options.to_capabilities()
self.service = Service(executable_path, port=port,
service_args=service_args, log_path=service_log_path)
self.service.start()
try:
RemoteWebDriver.__init__(self,
command_executor=self.service.service_url,
desired_capabilities=desired_capabilities)
except:
self.quit()
raise
self._is_remote = False
all that changed was the "skip_capabilities_update" kwarg. Now I just do this to set my own dict:
capabilities = dict( DesiredCapabilities.CHROME )
if not "chromeOptions" in capabilities:
capabilities['chromeOptions'] = {
'args' : [],
'binary' : "",
'extensions' : [],
'prefs' : {}
}
capabilities['proxy'] = {
'httpProxy' : "%s:%i" %(proxy_address, proxy_port),
'ftpProxy' : "%s:%i" %(proxy_address, proxy_port),
'sslProxy' : "%s:%i" %(proxy_address, proxy_port),
'noProxy' : None,
'proxyType' : "MANUAL",
'class' : "org.openqa.selenium.Proxy",
'autodetect' : False
}
driver = webdriver.Chrome( executable_path="path_to_chrome", desired_capabilities=capabilities, skip_capabilities_update=True )
It's easy!
First, define your proxy url
proxy_url = "127.0.0.1:9009"
proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': proxy_url,
'sslProxy': proxy_url,
'noProxy': ''})
Then, create chrome capability settings and add proxy to them
capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)
Finally, create the webdriver and pass the desired capabilities
driver = webdriver.Chrome(desired_capabilities=capabilities)
driver.get("http://example.org")
All together it looks like this
from selenium import webdriver
from selenium.webdriver.common.proxy import *
proxy_url = "127.0.0.1:9009"
proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': proxy_url,
'sslProxy': proxy_url,
'noProxy': ''})
capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)
driver = webdriver.Chrome(desired_capabilities=capabilities)
driver.get("http://example.org")
This worked for me like a charm:
proxy = "localhost:8080"
desired_capabilities = webdriver.DesiredCapabilities.CHROME.copy()
desired_capabilities['proxy'] = {
"httpProxy": proxy,
"ftpProxy": proxy,
"sslProxy": proxy,
"noProxy": None,
"proxyType": "MANUAL",
"class": "org.openqa.selenium.Proxy",
"autodetect": False
}
For people out there asking how to setup proxy server in chrome which needs authentication should follow these steps.
Create a proxy.py file in your project, use this code and call proxy_chrome from
proxy.py everytime you need it. You need to pass parameters like proxy server, port and username password for authentication.
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
import zipfile,os
def proxy_chrome(PROXY_HOST,PROXY_PORT,PROXY_USER,PROXY_PASS):
manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}
"""
background_js = """
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "https",
host: "%(host)s",
port: parseInt(%(port)d)
},
bypassList: ["foobar.com"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "%(user)s",
password: "%(pass)s"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
""" % {
"host": PROXY_HOST,
"port": PROXY_PORT,
"user": PROXY_USER,
"pass": PROXY_PASS,
}
pluginfile = 'extension/proxy_auth_plugin.zip'
with zipfile.ZipFile(pluginfile, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
co = Options()
#extension support is not possible in incognito mode for now
#co.add_argument('--incognito')
co.add_argument('--disable-gpu')
#disable infobars
co.add_argument('--disable-infobars')
co.add_experimental_option("excludeSwitches",["ignore-certificate-errors"])
#location of chromedriver, please change it according to your project.
chromedriver = os.getcwd()+'/Chromedriver/chromedriver'
co.add_extension(pluginfile)
driver = webdriver.Chrome(chromedriver,chrome_options=co)
#return the driver with added proxy configuration.
return driver
from selenium import webdriver
from selenium.webdriver.common.proxy import *
myProxy = "86.111.144.194:3128"
proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': myProxy,
'ftpProxy': myProxy,
'sslProxy': myProxy,
'noProxy':''})
driver = webdriver.Firefox(proxy=proxy)
driver.set_page_load_timeout(30)
driver.get('http://whatismyip.com')

Categories