Seleniumwire and selenium-hub with proxy

23 views Asked by At

I need help with using a proxy in seleniumwire Remote driver.

I'am tried using a proxy in default seleniumwire driver (Chrome, Firefox) and everything be fine.

My class for work with selenium:

class SeleniumWorker:
    def __init__(self):
        self.driver: webdriver.Remote = None
        self.is_initialized = False
        self.chrome_options = None
        self.selenium_wire_options = {}
        self.capabilities = {}

    def initialize(self, proxy: str):
        self.chrome_options = webdriver.ChromeOptions()
        self.chrome_options.add_argument(f'--proxy-server=0.0.0.0:4444')
        self.chrome_options.add_argument('--ignore-certificate-errors')

        self.selenium_wire_options = {
            'proxy': {
                # I tried to use socks5 and http
                'http': proxy,  
                'https': proxy
                'no_proxy': 'localhost,127.0.0.1'
            },
            'autoconf': True,
            'addr': '0.0.0.0',
            'port': SELENIUM_DRIVER_PORT,
        }

        self.capabilities = {
            "browserName": "chrome",
            "version": "latest",
            "platform": "ANY",
            "javascriptEnabled": True,
            "goog:chromeOptions": {
                'args': ['--ignore-certificate-errors']
            }
        }

        self.driver = webdriver.Remote(
            command_executor=f"http://0.0.0.0:4444",  # it's ok
            seleniumwire_options=self.selenium_wire_options,
            options=self.chrome_options,
            desired_capabilities=self.capabilities
        )

    def run(self, url: str, proxy: str):
        try:
            self.initialize(proxy)
            self.driver.get(url)
            return self.driver.page_source
        except Exception as ex:
            logger.error(f"Ошибка отправки запроса [{ex}]")
            self.__del__()

    def __del__(self):
        try:
            if self.driver:
                self.driver.close()
                self.driver.quit()
        except Exception as ex:
            logger.error(f"Ошибка очистки {ex}")

Errors which i are getting:

  1. 400 Bad request (from seleniumwire)
  2. Connection refused (if i use docker container name).
0

There are 0 answers