def get_webdriver()

in textworld/render/render.py [0:0]


def get_webdriver(path=None):
    """
    Get the driver and options objects.
    :param path: path to browser binary.
    :return: driver
    """
    check_modules(["selenium", "webdriver"], missing_modules)

    def chrome_driver(path=None):
        import urllib3
        from selenium.webdriver.chrome.options import Options
        options = Options()
        options.add_argument('headless')
        options.add_argument('ignore-certificate-errors')
        options.add_argument("test-type")
        options.add_argument("no-sandbox")
        options.add_argument("disable-gpu")
        options.add_argument("allow-insecure-localhost")
        options.add_argument("allow-running-insecure-content")

        if path is not None:
            options.binary_location = path

        SELENIUM_RETRIES = 10
        SELENIUM_DELAY = 3  # seconds
        for _ in range(SELENIUM_RETRIES):
            try:
                return webdriver.Chrome(chrome_options=options)
            except urllib3.exceptions.ProtocolError:  # https://github.com/SeleniumHQ/selenium/issues/5296
                time.sleep(SELENIUM_DELAY)

        raise ConnectionResetError('Cannot connect to Chrome, giving up after {SELENIUM_RETRIES} attempts.')

    def firefox_driver(path=None):
        from selenium.webdriver.firefox.options import Options
        options = Options()
        options.add_argument('headless')
        driver = webdriver.Firefox(firefox_binary=path, options=options)
        return driver

    driver_mapping = {
        'geckodriver': firefox_driver,
        'chromedriver': chrome_driver,
        'chromium-driver': chrome_driver
    }

    for driver in sorted(driver_mapping.keys()):
        found = which(driver)
        if found is not None:
            return driver_mapping.get(driver, None)(path)

    raise WebdriverNotFoundError("Chrome/Chromium/FireFox Webdriver not found.")