Как я могу изменить прокси-сервер webdriver через расширение Chrome, не закрывая веб-драйвер?

class WebDriver:
def __init__(self, host, port, user, passw):
    self.host = host
    self.port = port
    self.user = user
    self.passw = passw
    self.manifest_json = """
        {
            "version": "1.0.0",
            "manifest_version": 2,
            "name": "Proxy change",
            "permissions": [
                "proxy",
                "tabs",
                "unlimitedStorage",
                "storage",
                "<all_urls>",
                "webRequest",
                "webRequestBlocking"
            ],
            "background": {
                "scripts": ["background.js"]
            },
            "minimum_chrome_version":"22.0.0"
        }
        """
    self.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']
        );
        """ % (self.host, self.port, self.user, self.passw)

def get_chromedriver(self, 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", self.manifest_json)
            zp.writestr("background.js", self.background_js)
        chrome_options.add_extension(pluginfile)
    if user_agent:
        chrome_options.add_argument('--user-agent=%s' % user_agent)
    chrome_options.add_experimental_option('excludeSwitches', ['enable-logging']);
    driver = webdriver.Chrome(
        os.path.join(path, 'chromedriver'),
        options=chrome_options)
    return driver

Я пытаюсь создать расширение, которое динамически изменяет прокси-сервер webdriver. В основном я стремлюсь изменить его с помощью функции driver.execute_script (функция в расширении, которая изменяет прокси, который находится в файле background.js). Итак, вот мой вопрос: как я могу общаться с background.js, чтобы я мог использовать функцию изменения прокси прямо из консоли devtools в веб-драйвере (driver.execute_script ()).


person hen    schedule 25.07.2020    source источник