Exemplo n.º 1
0
def main():
    args = parse_command_line()
    for browser in args.browser:
        if ':' in browser:
            browser, version = browser.split(':')
        else:
            version = 'latest'
        if browser.lower() in DOWNLOADERS.keys():
            print('Downloading WebDriver for browser: "{0}"'.format(browser))
            downloader = DOWNLOADERS[browser](args.downloadpath, args.linkpath, args.os_name, args.bitness)
            try:
                extracted_binary, link = downloader.download_and_install(version)
            except ConnectionError:
                print("Unable to download webdriver's at this time due to network connectivity error")
                sys.exit(1)
            print('Driver binary downloaded to: "{0}"'.format(extracted_binary))
            if link is not None:
                if os.path.islink(link):
                    print('Symlink created: {0}'.format(link))
                else:
                    print('Driver copied to: {0}'.format(link))
                link_path = os.path.split(link)[0]
                if link_path not in os.environ['PATH'].split(os.pathsep):
                    print('WARNING: Path "{0}" is not in the PATH environment variable.'.format(link_path))
            else:
                print('Linking webdriver skipped')
        else:
            print('Unrecognized browser: "{0}".  Ignoring...'.format(browser))
        print('')
Exemplo n.º 2
0
def main():
    args = parse_command_line()
    for browser in args.browser:

        if ":" in browser:
            browser, version = browser.split(":")
        else:
            version = "compatible"

        if browser.lower() in DOWNLOADERS.keys():
            print(f'Downloading WebDriver for browser: "{browser}"')
            downloader = DOWNLOADERS[browser](args.downloadpath, args.linkpath, args.os_name, args.bitness)

            try:
                extracted_binary, link = downloader.download_and_install(version)
            except ConnectionError:
                print("Unable to download webdriver's at this time due to network connectivity error")
                sys.exit(1)

            print(f'Driver binary downloaded to: "{extracted_binary}"')
            if link:
                if link.is_symlink():
                    print(f"Symlink created: {link}")
                else:
                    print(f"Driver copied to: {link}")
                link_path = link.parent
                if str(link_path) not in os.environ["PATH"].split(os.pathsep):
                    print(f'WARNING: Path "{link_path}" is not in the PATH environment variable.')
            else:
                print("Linking webdriver skipped")
        else:
            print('Unrecognized browser: "{browser}".  Ignoring...')
        print("")
Exemplo n.º 3
0
def executable(browser: str, download: bool = False) -> str:
    """Get path to webdriver executable, and download it if requested.

    :param browser: name of browser to get webdriver for
    :param download: download driver binaries if they don't exist
    """

    LOGGER.debug(
        "Webdriver initialization for '%s' (download: %s)",
        browser,
        download,
    )

    browser = browser.lower().strip()
    factory = AVAILABLE_DRIVERS.get(browser)

    if not factory:
        return None

    driver_path = _driver_path(factory, download)

    if not download:
        LOGGER.info("Using already existing driver at: %s", driver_path)
    elif not driver_path.exists():
        _download_driver(factory)
    elif browser == "chrome":
        chrome_version = _chrome_version()
        driver_version = _chromedriver_version(driver_path)
        if chrome_version != driver_version:
            _download_driver(factory)
    else:
        LOGGER.info("Driver download skipped, because it exists at '%s'",
                    driver_path)

    return str(driver_path)
Exemplo n.º 4
0
def _to_manager(browser: str, root: Path = DRIVER_ROOT):
    browser = browser.strip()
    factory = AVAILABLE_DRIVERS.get(browser.lower())

    if not factory:
        raise ValueError(f"Unsupported browser: {browser}")

    manager = factory(download_root=root, link_path=root)
    return manager
Exemplo n.º 5
0
def parse_command_line():
    parser = argparse.ArgumentParser(
        description=
        'Tool for downloading and installing WebDriver binaries. Version: {}'.
        format(get_versions()["version"]), )
    parser.add_argument(
        'browser',
        help=
        'Browser to download the corresponding WebDriver binary.  Valid values are: {0}. Optionally specify a version number of the WebDriver binary as follows: \'browser:version\' e.g. \'chrome:2.39\'.  If no version number is specified, the latest available version of the WebDriver binary will be downloaded.'
        .format(', '.join(DOWNLOADERS.keys())),
        nargs='+')
    parser.add_argument('--downloadpath',
                        '-d',
                        action='store',
                        dest='downloadpath',
                        metavar='F',
                        default=None,
                        help='Where to download the webdriver binaries')
    parser.add_argument(
        '--linkpath',
        '-l',
        action='store',
        dest='linkpath',
        metavar='F',
        default=None,
        help=
        'Where to link the webdriver binary to. Set to "AUTO" if you need some intelligense to decide where to place the final webdriver binary. If set to "SKIP", no link/copy done.'
    )
    parser.add_argument(
        '--os',
        '-o',
        action='store',
        dest='os_name',
        choices=OS_NAMES,
        metavar='OSNAME',
        default=None,
        help='Overrides os detection with given os name. Values: {0}'.format(
            ', '.join(OS_NAMES)))
    parser.add_argument(
        '--bitness',
        '-b',
        action='store',
        dest='bitness',
        choices=BITNESS,
        metavar='BITS',
        default=None,
        help='Overrides bitness detection with given value. Values: {0}'.
        format(', '.join(BITNESS)))
    parser.add_argument('--version',
                        action='version',
                        version='%(prog)s {}'.format(
                            get_versions()["version"]))
    return parser.parse_args()
Exemplo n.º 6
0
    def webdriver_init(self, browser: str, download: bool = False) -> str:
        """Webdriver initialization with default driver
        paths or with downloaded drivers.

        :param browser: use drivers for this browser
        :param download: if True drivers are downloaded, not if False
        :return: path to driver or `None`
        """
        self.logger.debug(
            "Webdriver initialization for browser: '%s'. Download set to: %s",
            browser,
            download,
        )
        browser_version = self.detect_chrome_version()
        dm_class = (
            AVAILABLE_DRIVERS[browser.lower()]
            if browser.lower() in AVAILABLE_DRIVERS.keys()
            else None
        )
        if dm_class:
            self.logger.debug("Driver manager class: %s", dm_class)
            driver_ex_path, driver_tempdir = self._set_driver_paths(dm_class, download)
            if download:
                if not driver_ex_path.exists():
                    self.download_driver(dm_class, driver_tempdir, browser_version)
                else:
                    if browser.lower() == "chrome":
                        force_download = self._check_chrome_and_driver_versions(
                            driver_ex_path, browser_version
                        )
                        if force_download:
                            self.download_driver(
                                dm_class, driver_tempdir, browser_version
                            )
                    else:
                        self.logger.info(
                            "Driver download skipped, because it already "
                            "existed at %s",
                            driver_ex_path,
                        )
            else:
                self.logger.info("Using already existing driver at: %s", driver_ex_path)

            return r"%s" % str(driver_ex_path)
        else:
            return None
Exemplo n.º 7
0
def executable(browser: str, download: bool = False) -> Optional[str]:
    """Get path to webdriver executable, and download it if requested.

    :param browser: name of browser to get webdriver for
    :param download: download driver binaries if they don't exist
    """
    LOGGER.debug(
        "Webdriver initialization for '%s' (download: %s)",
        browser,
        download,
    )

    browser = browser.lower().strip()
    factory = AVAILABLE_DRIVERS.get(browser)
    if not factory:
        return None

    driver_path = _driver_path(factory, download)
    if driver_path is None:
        LOGGER.debug("Failed to get driver path for %s", browser)
        return None

    if driver_path.exists() and not download:
        LOGGER.debug("Attempting to use existing driver: %s", driver_path)
        return str(driver_path)
    elif driver_path.exists() and download:
        # TODO: Implement version check for all browsers
        if browser == "chrome":
            chrome_version = _chrome_version()
            driver_version = _chromedriver_version(driver_path)
            if chrome_version != driver_version:
                _download_driver(factory)
        else:
            LOGGER.debug("Driver download skipped, because it exists at '%s'",
                         driver_path)
        return str(driver_path)
    elif not driver_path.exists() and download:
        _download_driver(factory)
        return str(driver_path)
    else:
        LOGGER.debug("Attempting to use driver from PATH")
        return None