Пример #1
0
def check_driver_version(driver_path: str = None,
                         chrome_path: str = None) -> bool:
    """检查传入的chrome和chromedriver是否匹配  \n
    :param driver_path: chromedriver.exe路径
    :param chrome_path: chrome.exe路径
    :return: 是否匹配
    """
    print('正在检测可用性...')
    om = OptionsManager()
    driver_path = driver_path or om.get_value(
        'paths', 'chromedriver_path') or 'chromedriver'
    chrome_path = str(chrome_path
                      or om.get_value('chrome_options', 'binary_location'))
    do = DriverOptions(read_file=False)
    do.add_argument('--headless')

    if chrome_path:
        do.binary_location = chrome_path

    try:
        driver = webdriver.Chrome(driver_path, options=do)
        driver.quit()
        print('版本匹配,可正常使用。')

        return True

    except Exception as e:
        print(f'出现异常:\n{e}\n可执行easy_set.get_match_driver()自动下载匹配的版本。\n'
              f'或自行从以下网址下载:https://chromedriver.chromium.org/downloads')

        return False
Пример #2
0
def check_driver_version(driver_path: str = None,
                         chrome_path: str = None) -> bool:
    """检查传入的chrome和chromedriver是否匹配"""
    print('正在检测可用性...')
    om = OptionsManager()
    driver_path = driver_path or om.get_value(
        'paths', 'chromedriver_path') or 'chromedriver'
    chrome_path = chrome_path or om.get_value('chrome_options',
                                              'binary_location')
    do = DriverOptions(read_file=False)
    do.add_argument('--headless')
    if chrome_path:
        do.binary_location = chrome_path
    try:
        driver = webdriver.Chrome(driver_path, options=do)
        driver.quit()
        print('版本匹配,可正常使用。')
        return True
    except Exception as e:
        info = f'''
出现异常:
{e}chromedriver下载网址:https://chromedriver.chromium.org/downloads
'''
        print(info)
        return False
Пример #3
0
def check_driver_version(driver_path: str = None,
                         chrome_path: str = None) -> bool:
    om = OptionsManager()
    driver_path = driver_path or om.get_value(
        'paths', 'chromedriver_path') or 'chromedriver'
    chrome_path = chrome_path or om.get_value('chrome_options',
                                              'binary_location')
    do = DriverOptions(read_file=False)
    do.add_argument('--headless')
    if chrome_path:
        do.binary_location = chrome_path
    try:
        driver = webdriver.Chrome(driver_path, options=do)
        driver.quit()
        print('版本匹配,可正常使用。')
        return True
    except Exception as e:
        r = re.search(r'chromedriver=(.+?) ', str(e))
        info = f'''
版本不兼容。
请下载与当前chrome版本匹配的chromedriver。
当前chromedriver版本:{r.group(1)}
查看chrome版本方法:帮助 -> 关于Google Chrome
chromedriver下载网址:https://chromedriver.chromium.org/downloads
'''
        print(info)
        return False
Пример #4
0
def set_paths(driver_path: str = None,
              chrome_path: str = None,
              debugger_address: str = None,
              global_tmp_path: str = None,
              download_path: str = None) -> None:
    """简易设置路径函数
    :param driver_path: chromedriver.exe路径
    :param chrome_path: chrome.exe路径
    :param debugger_address: 调试浏览器地址,例:127.0.0.1:9222
    :param download_path: 下载文件路径
    :param global_tmp_path: 临时文件夹路径
    :return: None
    """
    om = OptionsManager()
    if driver_path is not None:
        om.set_item('paths', 'chromedriver_path', driver_path)
    if chrome_path is not None:
        om.set_item('chrome_options', 'binary_location', chrome_path)
    if debugger_address is not None:
        om.set_item('chrome_options', 'debugger_address', debugger_address)
    if global_tmp_path is not None:
        om.set_item('paths', 'global_tmp_path', global_tmp_path)
    if download_path is not None:
        experimental_options = om.get_value('chrome_options',
                                            'experimental_options')
        experimental_options['prefs'][
            'download.default_directory'] = download_path
        om.set_item('chrome_options', 'experimental_options',
                    experimental_options)
    om.save()
    check_driver_version(driver_path, chrome_path)
Пример #5
0
def set_paths(driver_path: str = None,
              chrome_path: str = None,
              debugger_address: str = None,
              tmp_path: str = None,
              download_path: str = None,
              user_data_path: str = None,
              cache_path: str = None,
              ini_path: str = None,
              check_version: bool = True) -> None:
    """快捷的路径设置函数                                          \n
    :param driver_path: chromedriver.exe路径
    :param chrome_path: chrome.exe路径
    :param debugger_address: 调试浏览器地址,例:127.0.0.1:9222
    :param download_path: 下载文件路径
    :param tmp_path: 临时文件夹路径
    :param user_data_path: 用户数据路径
    :param cache_path: 缓存路径
    :param ini_path: 要修改的ini文件路径
    :param check_version: 是否检查chromedriver和chrome是否匹配
    :return: None
    """
    om = OptionsManager(ini_path)

    def format_path(path: str) -> str:
        return '' if not path else str(path).replace('/', '\\')

    if driver_path is not None:
        om.set_item('paths', 'chromedriver_path', format_path(driver_path))

    if chrome_path is not None:
        om.set_item('chrome_options', 'binary_location',
                    format_path(chrome_path))

    if debugger_address is not None:
        om.set_item('chrome_options', 'debugger_address',
                    format_path(debugger_address))

    if tmp_path is not None:
        om.set_item('paths', 'tmp_path', format_path(tmp_path))

    if download_path is not None:
        experimental_options = om.get_value('chrome_options',
                                            'experimental_options')
        experimental_options['prefs'][
            'download.default_directory'] = format_path(download_path)
        om.set_item('chrome_options', 'experimental_options',
                    experimental_options)

    om.save()

    if user_data_path is not None:
        set_argument('--user-data-dir', format_path(user_data_path), ini_path)

    if cache_path is not None:
        set_argument('--disk-cache-dir', format_path(cache_path), ini_path)

    if check_version:
        check_driver_version(format_path(driver_path),
                             format_path(chrome_path))
Пример #6
0
def set_paths(driver_path: str = None,
              chrome_path: str = None,
              debugger_address: str = None,
              tmp_path: str = None,
              download_path: str = None,
              user_data_path: str = None,
              cache_path: str = None,
              check_version: bool = True) -> None:
    """简易设置路径函数
    :param driver_path: chromedriver.exe路径
    :param chrome_path: chrome.exe路径
    :param debugger_address: 调试浏览器地址,例:127.0.0.1:9222
    :param download_path: 下载文件路径
    :param tmp_path: 临时文件夹路径
    :param user_data_path: 用户数据路径
    :param cache_path: 缓存路径
    :param check_version: 是否检查chromedriver和chrome是否匹配
    :return: None
    """
    om = OptionsManager()
    if driver_path is not None:
        om.set_item('paths', 'chromedriver_path', driver_path)
    if chrome_path is not None:
        om.set_item('chrome_options', 'binary_location', chrome_path)
    if debugger_address is not None:
        om.set_item('chrome_options', 'debugger_address', debugger_address)
    if tmp_path is not None:
        om.set_item('paths', 'global_tmp_path', tmp_path)
    if download_path is not None:
        experimental_options = om.get_value('chrome_options',
                                            'experimental_options')
        experimental_options['prefs'][
            'download.default_directory'] = download_path
        om.set_item('chrome_options', 'experimental_options',
                    experimental_options)
    om.save()
    if user_data_path is not None:
        set_argument('--user-data-dir', user_data_path)
    if cache_path is not None:
        set_argument('--disk-cache-dir', cache_path)
    if check_version:
        check_driver_version(driver_path, chrome_path)