From 7b03820fa6e5b64622f26c63238ee7d1a7195b0d Mon Sep 17 00:00:00 2001 From: g1879 Date: Fri, 25 Feb 2022 15:10:29 +0800 Subject: [PATCH] =?UTF-8?q?2.5.4=20=E8=87=AA=E5=8A=A8=E8=8E=B7=E5=8F=96chr?= =?UTF-8?q?omedriver=E5=8A=9F=E8=83=BD=E9=80=82=E9=85=8D=E6=BA=90=E6=94=B9?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/easy_set.py | 43 ++++++++++++----------------------- docs/使用方法/查找页面元素.md | 6 +++-- docs/入门指南/快速上手.md | 2 +- setup.py | 2 +- 4 files changed, 21 insertions(+), 32 deletions(-) diff --git a/DrissionPage/easy_set.py b/DrissionPage/easy_set.py index 04cc4e1..9f64251 100644 --- a/DrissionPage/easy_set.py +++ b/DrissionPage/easy_set.py @@ -7,7 +7,7 @@ from os import popen from pathlib import Path from pprint import pprint -from re import search as RE_SEARCH, sub +from re import search from typing import Union from selenium import webdriver @@ -189,7 +189,7 @@ def check_driver_version(driver_path: str = None, chrome_path: str = None) -> bo except Exception as e: print(f'出现异常:\n{e}\n可执行easy_set.get_match_driver()自动下载匹配的版本。\n' - f'或自行从以下网址下载:https://chromedriver.chromium.org/downloads') + f'或自行从以下网址下载:http://npm.taobao.org/mirrors/chromedriver/') return False @@ -275,7 +275,8 @@ def _get_chrome_path(ini_path: str = None, import winreg try: key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, - r'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe', + # r'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe', + r'HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon\version', reserved=0, access=winreg.KEY_READ) k = winreg.EnumValue(key, 0) winreg.CloseKey(key) @@ -291,7 +292,7 @@ def _get_chrome_path(ini_path: str = None, # -----------从系统变量中获取-------------- if from_system_path: paths = popen('set path').read().lower() - r = RE_SEARCH(r'[^;]*chrome[^;]*', paths) + r = search(r'[^;]*chrome[^;]*', paths) if r: path = Path(r.group(0)) if 'chrome.exe' in r.group(0) else Path(r.group(0)) / 'chrome.exe' @@ -340,38 +341,24 @@ def _download_driver(version: str, save_path: str = None, show_msg: bool = True) if not version: return - page = SessionPage(Drission().session) - page.get('http://npm.taobao.org/mirrors/chromedriver') - + main_ver = version.split('.')[0] remote_ver = None - loc_main = version.split('.')[0] - try: - loc_num = int(version.replace('.', '')) - except ValueError: - return None + page = SessionPage(Drission().session) + page.get('https://registry.npmmirror.com/-/binary/chromedriver/') - remote_versions = page.eles(f'xpath://pre/a[starts-with(text(),"{loc_main}.")]') - remote_versions = [v for v in remote_versions if v.text and v.text[-1] == '/'] - remote_versions.sort(key=lambda x: x.text) - - for i in remote_versions: - try: - remote_num = int(sub(r'[./]', '', i.text)) - except ValueError: + for version in page.json: + # 遍历所有版本,跳过大版本不一致的,如果有完全匹配的,获取url,如果没有,获取最后一个版本的url + if not version['name'].startswith(f'{main_ver}.'): continue - if remote_num >= loc_num: - remote_ver = i.text + remote_ver = version['name'] + if version['name'] == f'{version}/': break - # 没有匹配到,则取大版本的最后一个号 - if remote_versions and not remote_ver: - remote_ver = remote_versions[-1].text - if remote_ver: - url = f'https://cdn.npm.taobao.org/dist/chromedriver/{remote_ver}chromedriver_win32.zip' - save_path = save_path or Path(__file__).parent + url = f'https://cdn.npmmirror.com/binaries/chromedriver/{remote_ver}chromedriver_win32.zip' + save_path = save_path or str(Path(__file__).parent) result = page.download(url, save_path, file_exists='overwrite', show_msg=show_msg) if result[0]: diff --git a/docs/使用方法/查找页面元素.md b/docs/使用方法/查找页面元素.md index 159fa17..5095fec 100644 --- a/docs/使用方法/查找页面元素.md +++ b/docs/使用方法/查找页面元素.md @@ -398,12 +398,14 @@ txt = ele1.ele('xpath://div/@class') 查找方法能直接接收 selenium 原生定位元组进行查找,s 模式下也支持这种写法。 ```python +from selenium.webdriver.common.by import By + # 查找 id 为 ele_id 的元素 -loc1 = By.ID, 'ele_id' +loc1 = (By.ID, 'ele_id') ele = page.ele(loc1) # 按 xpath 查找 -loc2 = By.XPATH, '//div[@class="ele_class"]' +loc2 = (By.XPATH, '//div[@class="ele_class"]' ele = page.ele(loc2) ``` diff --git a/docs/入门指南/快速上手.md b/docs/入门指南/快速上手.md index 89f7c90..7846ac4 100644 --- a/docs/入门指南/快速上手.md +++ b/docs/入门指南/快速上手.md @@ -58,7 +58,7 @@ set_paths(driver_path=r"D:\chrome\chromedriver.exe", 各版本 driver 下载地址: - 国外:https://chromedriver.chromium.org/downloads -- 国内:http://npm.taobao.org/mirrors/chromedriver +- 国内:http://npm.taobao.org/mirrors/chromedriver/ # 上手示例 diff --git a/setup.py b/setup.py index 6e3ab28..b86c8c7 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh: setup( name="DrissionPage", - version="2.5.3", + version="2.5.4", author="g1879", author_email="g1879@qq.com", description="A module that integrates selenium and requests session, encapsulates common page operations.",