Setting chromedriver proxy auth with Selenium using Python - 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.

Related

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 to initiate a Tor Browser 9.5 which uses the default Firefox to 68.9.0esr using GeckoDriver and Selenium through Python

I'm trying to initiate a tor browsing session through Tor Browser 9.5 which uses the default Firefox v68.9.0esr using GeckoDriver and Selenium through Python on a windows-10 system. But I'm facing an error as:
Code Block:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
import os
torexe = os.popen(r'C:\Users\username\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
profile = FirefoxProfile(r'C:\Users\username\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default')
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference("network.proxy.socks_remote_dns", False)
profile.update_preferences()
firefox_options = webdriver.FirefoxOptions()
firefox_options.binary_location = r'C:\Users\username\Desktop\Tor Browser\Browser\firefox.exe'
driver = webdriver.Firefox(firefox_profile= profile, options = firefox_options, executable_path=r'C:\WebDrivers\geckodriver.exe')
driver.get("https://www.tiktok.com/")
Where as the same code block works through Firefox and Firefox Nightly using the respective binaries.
Do I need any additional settings? Can someone help me out?
Firefox Snapshot:
Firefox Nightly Snapshot:
I managed to resolve this by updating to v9.5.1 and implementing the following changes:
Note that although the code is in C# the same changes to the Tor browser and how it is launched should be applied.
FirefoxProfile profile = new FirefoxProfile(profilePath);
profile.SetPreference("network.proxy.type", 1);
profile.SetPreference("network.proxy.socks", "127.0.0.1");
profile.SetPreference("network.proxy.socks_port", 9153);
profile.SetPreference("network.proxy.socks_remote_dns", false);
FirefoxDriverService firefoxDriverService = FirefoxDriverService.CreateDefaultService(geckoDriverDirectory);
firefoxDriverService.FirefoxBinaryPath = torPath;
firefoxDriverService.BrowserCommunicationPort = 2828;
var firefoxOptions = new FirefoxOptions
{
Profile = null,
LogLevel = FirefoxDriverLogLevel.Trace
};
firefoxOptions.AddArguments("-profile", profilePath);
FirefoxDriver driver = new FirefoxDriver(firefoxDriverService, firefoxOptions);
driver.Navigate().GoToUrl("https://www.google.com");
Important notes:
The following TOR configs need to be changed in about:config :
marionette.enabled: true
marionette.port: set to an unused port, and set this value to firefoxDriverService.BrowserCommunicationPort in your code. This was set to 2828 in my example.
note:
I am not sure whether this really is the definite answer (thus, I'd really appreciate feedback)
solution:
I've managed to send a get request to the check tor page (https://check.torproject.org/) and it displayed an unknown IP to me (additionally, IPs differ if you repeat the request after a time)
Essentially, I've set up the chrome driver to run TOR. Here's the code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
tor_proxy = "127.0.0.1:9150"
chrome_options = Options()
chrome_options.add_argument("--test-type")
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('disable-infobars')
chrome_options.add_argument("--incognito")
chrome_options.add_argument('--proxy-server=socks5://%s' % tor_proxy)
driver = webdriver.Chrome(options=chrome_options)
driver.get('https://check.torproject.org/')
Because the driver is not in headless mode you can inspect the resulting page yourself. It should read:
"Congratulations. This browser is configured to use Tor. [IP Info]. However, it does not appear to be Tor Browser. Click here to go to the download page"
Make sure that the chromedriver.exe file is linked on the path or provide the path to the file as an argument to the driver.Chrome() function.
Edit: make sure TOR browser is running in the background, thanks #Abhishek Rai for pointing that out

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

Problems with setting up proxy for chrome using selenium 3.8.1

I used to set up proxy on chrome like in a code below, but when i updated to selenium 3.8.1 proxy stops working, i dont get any errors it just doesn't use proxy server and i dont know why. My chromedriver is also up to date.
options = webdriver.ChromeOptions()
options.add_argument('--proxy-server=192.99.55.120:3128')
driver = webdriver.Chrome(executable_path='C:\chromedriver_win32\chromedriver.exe', chrome_options=options)
driver.get("http://google.com/")
Would like to receive any advice, maybe alternative way to set up proxy for chromedriver.
If someone still interested, this is how i have finally solved the problem
from selenium.webdriver import Proxy
settings = {
"httpProxy": "192.99.55.120:3128",
"sslProxy": "192.99.55.120:3128"
}
proxy = Proxy(settings)
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
cap = DesiredCapabilities.CHROME.copy()
cap['platform'] = "WINDOWS"
cap['version'] = "10"
proxy.add_to_capabilities(cap)
from selenium.webdriver.chrome.webdriver import WebDriver as ChromeDriver
driver = ChromeDriver(desired_capabilities=cap, executable_path='C:\chromedriver_win32\chromedriver.exe')
try
options.add_argument('--proxy-server="http=192.99.55.120:3128;https=192.99.55.120:3128"')
also try running your chrome binary directly with these params to see whether it works or not
chrome.exe --proxy-server="http=192.99.55.120:3128"
If the navigator asks for the credentials username and password for the proxy and you need to handle this : (only if the alert come up)
driver.get("http://username:password#google.com/")

Using a http proxy with headless firefox in Selenium webdriver in Python

I'm using Firefox headless like this:
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium import webdriver
import os
import sys
# Set the MOZ_HEADLESS environment variable which casues Firefox to
# start in headless mode.
os.environ['MOZ_HEADLESS'] = '1'
# Select your Firefox binary.
binary = FirefoxBinary('/usr/bin/firefox', log_file=sys.stdout)
# Start selenium with the configured binary.
driver = webdriver.Firefox(firefox_binary=binary)
But now I want to add a http proxy that requires a user/password. After searching around, I tried the following:
from selenium.webdriver.common.proxy import Proxy, ProxyType
myProxy = "xx.xx.xx.xx:80"
proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': myProxy,
'ftpProxy': myProxy,
'sslProxy': myProxy,
'noProxy': '' # set this value as desired
})
driver = webdriver.Firefox(firefox_binary=binary, proxy=proxy)
I also tried
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", "xx.xx.xx.xx")
profile.set_preference("network.proxy.http_port", 80)
profile.update_preferences()
driver=webdriver.Firefox(firefox_binary=binary,firefox_profile=profile)
Finally, I tried adding "socksUsername" and "socksPassword" with the creds to the proxy, more out of desperation than any real hope.
Needless to say none of these works, and testing shows requests are still using my usual IP, not the proxy.
Also system-wide proxy is not an option in this case.
Where should the http proxy credentials live? How can I use a proxy with headless firefox?
Testing
driver.get("https://www.ipinfo.io");
driver.find_element_by_xpath('//h4/following-sibling::p').text
If your proxy requires a username and a password, you have to write it like that :
from selenium.webdriver.common.proxy import Proxy, ProxyType
myProxy = "username:password#proxyDomain:proxyPort"
proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': myProxy,
'ftpProxy': myProxy,
'sslProxy': myProxy,
'noProxy': '' # set this value as desired
})
driver = webdriver.Firefox(firefox_binary=binary, proxy=proxy)
Have you try setting up a profile manually with
./firefox --ProfileManager
manually setup the proxy and then load the profile you manually set
from selenium import webdriver
url = "https://mail.google.com"
fp = webdriver.FirefoxProfile('/Users/<username>/Library/Application Support/Firefox/Profiles/71v1uczn.default')
driver = webdriver.Firefox(fp)
You can try setting up an Environment variable "HTTP_PROXY" in following mnemonics:
http://<username>:<password>#<proxy_url>
Add your credentials separated by colon ':' before proxy url that is preceded by '#' for e.g.
http://username:password#proxy.com:8080/file.pac

Categories