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
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
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
def set_argument(arg: str, value: Union[bool, str]) -> None: """设置属性 :param arg: 属性名 :param value: 属性值,有值的属性传入值,没有的传入bool :return: """ do = DriverOptions() do.remove_argument(arg) if value: arg_str = arg if isinstance(value, bool) else f'{arg}={value}' do.add_argument(arg_str) do.save()
def set_argument(arg: str, value: Union[bool, str], ini_path: str = None) -> None: """设置浏览器配置argument属性 \n :param arg: 属性名 :param value: 属性值,有值的属性传入值,没有的传入bool :param ini_path: 要修改的ini文件路径 :return: None """ do = DriverOptions(ini_path=ini_path) do.remove_argument(arg) if value: arg_str = arg if isinstance(value, bool) else f'{arg}={value}' do.add_argument(arg_str) do.save()