Proxy authentication using selenium in Python

I urgently need to use Selenium with Authenticated Proxy. I saw that Selenium doesn’t have anything practical to do this, just proxies without authentication, if I run the argument to register the proxy it opens a pop-up for you to enter the username and password, this doesn’t work for me because I need to open 10 pages at the same time, with varying sizes and varying positions.

I tried some solutions for this, the first was to use something called “Selenium-wire”, but it has some problems, it is VERY SLOW to run the functions, all compilers to transform it into EXE identify it as Virus and Malware and it does not complete all the processes simply stops working and only completes the process for some windows.

Second attempt was using Seleniumbase and using its Driver(), it works fine, but I can’t position the pages when they are opened and it seems that it is very slow when opening the pages.

Third attempt was using an old code that is a native form of Selenium but when I run capabilities it seems to have changed the function and it didn’t work for me.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium import webdriver
proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = "username:password@ip:port"
proxy.ssl_proxy = "username:password@ip:port"
capabilities = webdriver.DesiredCapabilities.CHROME.copy()
proxy.add_to_capabilities(capabilities)
</code>
<code>from selenium.webdriver.common.proxy import Proxy, ProxyType from selenium import webdriver proxy = Proxy() proxy.proxy_type = ProxyType.MANUAL proxy.http_proxy = "username:password@ip:port" proxy.ssl_proxy = "username:password@ip:port" capabilities = webdriver.DesiredCapabilities.CHROME.copy() proxy.add_to_capabilities(capabilities) </code>
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium import webdriver

proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = "username:password@ip:port"
proxy.ssl_proxy = "username:password@ip:port"  
capabilities = webdriver.DesiredCapabilities.CHROME.copy()
proxy.add_to_capabilities(capabilities)

Fourth attempt is to use an extension and configure the proxy there, I managed it and it is working very well! But some windows are not opening in the language I need, I need it to open in Portuguese, but it opens mixed, sometimes in Portuguese and sometimes in English, it was the best solution so far, but my users are all Brazilians who speak Portuguese, I need the windows to open in Portuguese!

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> def create_chromedriver(PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS, USER_AGENT, pos_x, pos_y, altura, largura, posicao):
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']
);
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
for (var i = 0; i < details.requestHeaders.length; ++i) {
if (details.requestHeaders[i].name === 'Accept-Language') {
details.requestHeaders[i].value = 'pt-BR,pt;q=0.9';
break;
}
}
return {requestHeaders: details.requestHeaders};
},
{urls: ["<all_urls>"]},
["blocking", "requestHeaders"]
);
""" % (PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS)
def get_chromedriver(use_proxy=True, user_agent=USER_AGENT, x=pos_x, y=pos_y, alt=altura, larg=largura, posicao_janela=posicao):
path = os.path.dirname(os.path.abspath(__file__))
chrome_options = Options()
if use_proxy:
pluginfile = f'proxy_{posicao_janela}.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)
arguments = ['--disable-notifications', '--disable-popup-blocking', f"--window-position={x},{y}", f"--window-size={larg},{alt}"]
for argument in arguments:
chrome_options.add_argument(argument)
driver = webdriver.Chrome(options=chrome_options)
return driver
driver = get_chromedriver(use_proxy=True)
return driver
</code>
<code> def create_chromedriver(PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS, USER_AGENT, pos_x, pos_y, altura, largura, posicao): 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'] ); chrome.webRequest.onBeforeSendHeaders.addListener( function(details) { for (var i = 0; i < details.requestHeaders.length; ++i) { if (details.requestHeaders[i].name === 'Accept-Language') { details.requestHeaders[i].value = 'pt-BR,pt;q=0.9'; break; } } return {requestHeaders: details.requestHeaders}; }, {urls: ["<all_urls>"]}, ["blocking", "requestHeaders"] ); """ % (PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS) def get_chromedriver(use_proxy=True, user_agent=USER_AGENT, x=pos_x, y=pos_y, alt=altura, larg=largura, posicao_janela=posicao): path = os.path.dirname(os.path.abspath(__file__)) chrome_options = Options() if use_proxy: pluginfile = f'proxy_{posicao_janela}.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) arguments = ['--disable-notifications', '--disable-popup-blocking', f"--window-position={x},{y}", f"--window-size={larg},{alt}"] for argument in arguments: chrome_options.add_argument(argument) driver = webdriver.Chrome(options=chrome_options) return driver driver = get_chromedriver(use_proxy=True) return driver </code>
    def create_chromedriver(PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS, USER_AGENT, pos_x, pos_y, altura, largura, posicao):
        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']
        );

        chrome.webRequest.onBeforeSendHeaders.addListener(
            function(details) {
                for (var i = 0; i < details.requestHeaders.length; ++i) {
                    if (details.requestHeaders[i].name === 'Accept-Language') {
                        details.requestHeaders[i].value = 'pt-BR,pt;q=0.9';
                        break;
                    }
                }
                return {requestHeaders: details.requestHeaders};
            },
            {urls: ["<all_urls>"]},
            ["blocking", "requestHeaders"]
        );

        """ % (PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS)

        def get_chromedriver(use_proxy=True, user_agent=USER_AGENT, x=pos_x, y=pos_y, alt=altura, larg=largura, posicao_janela=posicao):
            path = os.path.dirname(os.path.abspath(__file__))
            chrome_options = Options()

            if use_proxy:
                pluginfile = f'proxy_{posicao_janela}.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)

            arguments = ['--disable-notifications', '--disable-popup-blocking', f"--window-position={x},{y}", f"--window-size={larg},{alt}"]

            for argument in arguments:
                chrome_options.add_argument(argument)

            driver = webdriver.Chrome(options=chrome_options)
            return driver

        driver = get_chromedriver(use_proxy=True)
        return driver

New contributor

Felipe Ataide is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật