Reputation: 149
am creating a script that opens multiple windows while every window use a unique authenticated proxy, I managed to do that but with free proxies but am having troubles while having a paid ones I used this solution to authenticate my proxies (how to set proxy with authentication in selenium chromedriver python?)but couldn't find a way to use this solution to adapt it with my code.
import os
import zipfile
import time
from selenium import webdriver
PROXY_HOST = 'host' # rotating proxy
PROXY_PORT = 'port'
PROXY_USER = 'user'
PROXY_PASS = '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: "http",
host: "%s",
port: parseInt(%s)
},
bypassList: ["localhost"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "%s",
password: "%s"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
""" % (PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS)
def get_chromedriver(use_proxy=False, user_agent=None):
path = os.path.dirname(os.path.abspath(__file__))
chrome_options = webdriver.ChromeOptions()
if use_proxy:
pluginfile = 'proxy_auth_plugin.zip'
with zipfile.ZipFile(pluginfile, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
chrome_options.add_extension(pluginfile)
if user_agent:
chrome_options.add_argument('--user-agent=%s' % user_agent)
driver = webdriver.Chrome('yourpath',options=chrome_options)
return driver
def main():
driver = get_chromedriver(use_proxy=True)
#driver.get('https://www.google.com/search?q=my+ip+address')
driver.get('https://whatismyipaddress.com/')
main()
Upvotes: 0
Views: 460
Reputation: 149
I found a solution by using selenium wire
from seleniumwire import webdriver
proxy_host = ['host', 'host', 'host', 'host'] # rotating proxy
proxy_port = ['port', 'port', 'port', 'port']
proxy_username = ['username', 'username', 'username', 'username']
proxy_password = ['password', 'password', 'password', 'password']
def new(username,password,host,port):
options = {
'proxy': {
'http': 'http://%s:%s@%s:%s'%(username,password,host,port),
'https': 'http://%s:%s@%s:%s'%(username,password,host,port),
'no_proxy': 'localhost,127.0.0.1' # excludes
}
}
return options
for username, password, host, port in zip(proxy_username, proxy_password, proxy_host, proxy_port):
driver = webdriver.Chrome(executable_path="your path",seleniumwire_options=new(username, password, host, port))
driver.get('https://whatismyipaddress.com/')
Upvotes: 0