How can I set socks5 proxy for selenium webdriver? Python - 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

Related

how to set selenium driver proxy

i try to use this code
# options.add_argument(
# f"proxy-server={t_proxy}".replace('https://', '').replace('http://', '')
# )
but failed
i look for the documents
https://www.selenium.dev/documentation/webdriver/drivers/options/#proxy
found this
from selenium import webdriver
PROXY = "<HOST:PORT>"
webdriver.DesiredCapabilities.FIREFOX['proxy'] = {
"httpProxy": PROXY,
"ftpProxy": PROXY,
"sslProxy": PROXY,
"proxyType": "MANUAL",
}
with webdriver.Firefox() as driver:
driver.get("https://selenium.dev")
but failed again
if i have a proxy ip now (222.168.1.1:5000)
how should i write this code?

Use Proxy with Authorization in Firefox with Selenium in Python

How to provide Username and Password to a proxy server in Firefox with selenium in python?
I tried the code below but the IP address doesn't change. I check the proxy setting in Firefox, it shows that only http proxy is changed, but not https.
proxy = {'host': '111.111.111.11',
'port': 8080,
'usr': USERNAME,
'pwd': PASSWORD
}
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", proxy['host'])
profile.set_preference("network.proxy.https", proxy['host'])
profile.set_preference("network.proxy.http_port", proxy['port'])
profile.set_preference("network.proxy.username", proxy['usr'])
profile.set_preference("network.proxy.password", proxy['pwd'])
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile)
driver.get('https://whatismyipaddress.com')
I see a solution for ChromeDriver (but I don't test it because I can only use Firefox):
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=196.19.8.80:8000')
chrome_options.add_argument('--proxy-auth=yjd5Rn:29apJZ')
chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://whatismyipaddress.com")
As you can see, ChromeOptions have '--proxy-auth=yjd5Rn:29apJZ' argument.
Could anyone tell me how to use proxy and provide authorization to proxy server in Firefox with Selenium and in python? Thanks in advance!
Below is the login page from the proxy server:

How do you run headless chrome and a proxy using selenium in python?

My python script has the headless chrome in selenium up and functional but how, if possible, can I use a proxy as well? How can I pass the proxy host port to my headless chrome browser?
options = webdriver.ChromeOptions()
options.add_argument('headless')
browser = webdriver.Chrome(chrome_options=options)
How do I use the proxy host port with selenium and mainly headless chrome? Thanks!
Something like this:
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
options = webdriver.ChromeOptions()
options.add_argument('headless')
desired_caps = options.to_capabilities()
prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = "ip_addr:port"
prox.socks_proxy = "ip_addr:port"
prox.ssl_proxy = "ip_addr:port"
prox.add_to_capabilities(desired_caps)
browser = webdriver.Chrome(desired_capabilities=desired_caps)
The simplest way to do is something like below. Put any ip address in the proxy variable to find out how it works. ip and port are separated by :.
from selenium import webdriver
proxy = "94.122.251.105:3128" #test with any ip address which supports `http` as well because the link within the script are of `http`
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--proxy-server={}'.format(proxy))
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('http://www.lagado.com/proxy-test')
items = driver.find_element_by_css_selector(".main-panel p:nth-of-type(2)").text
print(items) #it should print out the ip address you are using in the `proxy` variable above
driver.quit()

Using proxy with docker selenium python not working

I want to use proxy with username and password in my docker selenium container.
I tried every solution that I found on so and it doesn't work. I tried http proxy:
capabilities = DesiredCapabilities.FIREFOX
capabilities['proxy'] = {
'proxyType': 'MANUAL',
'httpProxy': f'{proxy.ip_address}:{proxy.port}',
'sslProxy': 'ip:port',
'socksUsername': proxy.login,
'socksPassword': proxy.password
}
browser = webdriver.Remote(command_executor='http://hub:4444/wd/hub',
desired_capabilities=capabilities,
browser_profile=profile)
selenium.common.exceptions.InvalidArgumentException: Message: Invalid
proxy configuration entry: socksPassword
tried socks proxy:
proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'socksProxy': f'{proxy.ip_address}:{proxy.port}',
'socksUsername': proxy.login,
'socksPassword': proxy.password
})
browser = webdriver.Remote(command_executor='http://hub:4444/wd/hub',
desired_capabilities=capabilities,
browser_profile=profile,
proxy=proxy)
Message: Invalid proxy configuration entry: socksPassword
I also tried to set proxy via firefox profile, like here, but it seems not working because there is no option to set password for proxy.
Proxy is working because when i do request:
proxy = 'socks5://username:password#ip:port' resp = requests.get('https://api.ipify.org?format=json',
proxies=dict(http=proxy,
https=proxy)) print(resp.json())
returns correct result
The only solution is using proxy without password
Python3. You need to pip3 install geckodriver
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
# https://free-proxy-list.net/
proxy = '78.96.125.24:3128'
firefox_capabilities['proxy'] = {
"proxyType": "MANUAL",
"httpProxy": proxy,
"ftpProxy": proxy,
"sslProxy": proxy,
}
browser = webdriver.Firefox(capabilities=firefox_capabilities)
browser.get('https://httpbin.org/ip')
# browser.get('https://www.google.pl')

Setting chromedriver proxy auth with Selenium using Python

I am coding a test suite using Python and the Selenium library. Using the chromedriver, I am setting proxies using:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % hostname + ":" + port)
global driver
driver = webdriver.Chrome(chrome_options=chrome_options)
This works fine when the proxy does not have authentication. However, if the proxy requires you to login with a username and password it will not work. What is the correct and proper way to pass proxy authentication information to the chromedriver using add_argument or other methods?
It is not the same as: How to set Proxy setting for Chrome in Selenium Java
Seeing as:
I ts a different language
Its firefox, not chrome.
--proxy-server=http://user:password#proxy.com:8080 does not work.
Use DesiredCapabilities. I have been successfully using proxy authentication with the following:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
proxy = {'address': '123.123.123.123:2345',
'username': 'johnsmith123',
'password': 'iliketurtles'}
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}
capabilities['proxy']['socksUsername'] = proxy['username']
capabilities['proxy']['socksPassword'] = proxy['password']
driver = webdriver.Chrome(executable_path=[path to your chromedriver], desired_capabilities=capabilities)
EDIT: it unfortunately seems this method no longer works since one of the updated to either Selenium or Chrome since this post. as of now, i do not know another solution, but i will experiment and update this if i find anything out.

Categories