R
R
r1dddy4sv2021-04-20 13:12:37
Python
r1dddy4sv, 2021-04-20 13:12:37

How to run Chrome in headless mode using selenium python authorization proxy?

I'm trying to run a proxy with login and password in chrome in headless mode like this:

try:
                try:
                    path = os.path.dirname(os.path.abspath(__file__))
                    os.remove(f'{path}\\Chrome\\Application\\89.0.4389.90\\Extensions\\proxy_auth_plugin.zip')
                except:
                    pass
                ZipFile(f'{path}\\Chrome\\Application\\89.0.4389.90\\Extensions\\proxy_auth_plugin.zip', 'w')
                def proxy_chrome(PROXY_HOST,PROXY_PORT,PROXY_USER,PROXY_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: "%(host)s",
                                port: parseInt(%(port)d)
                            },
                            bypassList: ["foobar.com"]
                            }
                        };
                    chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
                    function callbackFn(details) {
                        return {
                            authCredentials: {
                                username: "%(user)s",
                                password: "%(pass)s"
                            }
                        };
                    }
                    chrome.webRequest.onAuthRequired.addListener(
                                callbackFn,
                                {urls: ["<all_urls>"]},
                                ['blocking']
                    );
                        """ % {
                            "host": PROXY_HOST,
                            "port": int(PROXY_PORT),
                            "user": PROXY_USER,
                            "pass": PROXY_PASS,
                        }

                    path = os.path.dirname(os.path.abspath(__file__))
                    pluginfile = f'{path}\\Chrome\\Application\\89.0.4389.90\\Extensions\\proxy_auth_plugin.zip'

                    with zipfile.ZipFile(pluginfile, 'w') as zp:
                        zp.writestr("manifest.json", manifest_json)
                        zp.writestr("background.js", background_js)

                    path = os.path.dirname(os.path.abspath(__file__))
                    chrome_options = webdriver.ChromeOptions()
                    chrome_options.add_argument('--ignore-certificate-errors')
                    chrome_options.add_argument("--disable-application-cache")
                    chrome_options.add_argument("start-maximized")

                    chrome_options.headless = True

                    chrome_options.binary_location = os.path.join(path, r'Chrome\Application\chrome.exe')
                    chrome_options.add_extension(pluginfile)
                    driver = webdriver.Chrome(executable_path=os.path.join(path, 'chromedriver.exe'),chrome_options=chrome_options)
                    return driver
                PROXY_HOST'***'
                PROXY_PORT='***'
                PROXY_USER='***'
                PROXY_PASS='***'
                driver=proxy_chrome(PROXY_HOST,PROXY_PORT,PROXY_USER,PROXY_PASS)
            except Exception as x:
                print(repr(x))
                print('Что то не так с прокси')
                raise Exception('Что то не так с прокси')

but I get an error:
unknown error: failed to wait for extension background page to load: chrome-extension://icopbmefnonpflniijpjjponhlkcmiao/_generated_background_page.html
from unknown error: page could not be found: chrome-extension://icopbmefnonpflniijpjjponhlkcmiao/_generated_background_page.html


I would appreciate any help

Thank you

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zlo1, 2021-04-20
@zlo1

Use a local proxy server
3proxy intermediary can redirect requests to all types of proxies and with authorization

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question