B
B
Bergis2021-06-13 02:56:53
Python
Bergis, 2021-06-13 02:56:53

How to solve proxy problem in selenium?

Tell me how you can fix the error, due to which chrome does not use the proxy given to it

import os
import zipfile

from selenium import webdriver
import undetected_chromedriver.v2 as uc

PROXY_HOST = ''  # rotating proxy or host
PROXY_PORT =  # port
PROXY_USER = '' # username
PROXY_PASS = '' # password


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: "socks5",
            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__))
    options = uc.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)
        options.add_extension(pluginfile)
    if user_agent:
        options.add_argument('--user-agent=%s' % user_agent)
    driver = uc.Chrome(options=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://httpbin.org/ip')

if __name__ == '__main__':
    main()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
UberPool, 2021-06-13
@Bergis

Stop using the extension for this!
Use lib for this.
The code:

import chromedriver_binary
from selenium import webdriver
from seleniumwire import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager

chrome_options = Options()

chrome_options.add_argument("--headless")


username = ''
password = ''
ip = ''
port = 1

proxy = f'{username}:{password}@{ip}:{port}'
options = {

    'proxy': {
        'https': f'https://{proxy}',
    }
}
driver = webdriver.Chrome(ChromeDriverManager().install(),seleniumwire_options=options, options=chrome_options)
#уже с прокси делаешь что-то
#заходишь на сайты и тд

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question