2.5.4 自动获取chromedriver功能适配源改版

This commit is contained in:
g1879 2022-02-25 15:10:29 +08:00
parent 6738dd493c
commit 7b03820fa6
4 changed files with 21 additions and 32 deletions

View File

@ -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]:

View File

@ -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)
```

View File

@ -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/
# 上手示例

View File

@ -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.",