mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
2.5.4 自动获取chromedriver功能适配源改版
This commit is contained in:
parent
6738dd493c
commit
7b03820fa6
@ -7,7 +7,7 @@
|
|||||||
from os import popen
|
from os import popen
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
from re import search as RE_SEARCH, sub
|
from re import search
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
from selenium import webdriver
|
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:
|
except Exception as e:
|
||||||
print(f'出现异常:\n{e}\n可执行easy_set.get_match_driver()自动下载匹配的版本。\n'
|
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
|
return False
|
||||||
|
|
||||||
@ -275,7 +275,8 @@ def _get_chrome_path(ini_path: str = None,
|
|||||||
import winreg
|
import winreg
|
||||||
try:
|
try:
|
||||||
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,
|
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)
|
reserved=0, access=winreg.KEY_READ)
|
||||||
k = winreg.EnumValue(key, 0)
|
k = winreg.EnumValue(key, 0)
|
||||||
winreg.CloseKey(key)
|
winreg.CloseKey(key)
|
||||||
@ -291,7 +292,7 @@ def _get_chrome_path(ini_path: str = None,
|
|||||||
# -----------从系统变量中获取--------------
|
# -----------从系统变量中获取--------------
|
||||||
if from_system_path:
|
if from_system_path:
|
||||||
paths = popen('set path').read().lower()
|
paths = popen('set path').read().lower()
|
||||||
r = RE_SEARCH(r'[^;]*chrome[^;]*', paths)
|
r = search(r'[^;]*chrome[^;]*', paths)
|
||||||
|
|
||||||
if r:
|
if r:
|
||||||
path = Path(r.group(0)) if 'chrome.exe' in r.group(0) else Path(r.group(0)) / 'chrome.exe'
|
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:
|
if not version:
|
||||||
return
|
return
|
||||||
|
|
||||||
page = SessionPage(Drission().session)
|
main_ver = version.split('.')[0]
|
||||||
page.get('http://npm.taobao.org/mirrors/chromedriver')
|
|
||||||
|
|
||||||
remote_ver = None
|
remote_ver = None
|
||||||
loc_main = version.split('.')[0]
|
|
||||||
|
|
||||||
try:
|
page = SessionPage(Drission().session)
|
||||||
loc_num = int(version.replace('.', ''))
|
page.get('https://registry.npmmirror.com/-/binary/chromedriver/')
|
||||||
except ValueError:
|
|
||||||
return None
|
|
||||||
|
|
||||||
remote_versions = page.eles(f'xpath://pre/a[starts-with(text(),"{loc_main}.")]')
|
for version in page.json:
|
||||||
remote_versions = [v for v in remote_versions if v.text and v.text[-1] == '/']
|
# 遍历所有版本,跳过大版本不一致的,如果有完全匹配的,获取url,如果没有,获取最后一个版本的url
|
||||||
remote_versions.sort(key=lambda x: x.text)
|
if not version['name'].startswith(f'{main_ver}.'):
|
||||||
|
|
||||||
for i in remote_versions:
|
|
||||||
try:
|
|
||||||
remote_num = int(sub(r'[./]', '', i.text))
|
|
||||||
except ValueError:
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if remote_num >= loc_num:
|
remote_ver = version['name']
|
||||||
remote_ver = i.text
|
if version['name'] == f'{version}/':
|
||||||
break
|
break
|
||||||
|
|
||||||
# 没有匹配到,则取大版本的最后一个号
|
|
||||||
if remote_versions and not remote_ver:
|
|
||||||
remote_ver = remote_versions[-1].text
|
|
||||||
|
|
||||||
if remote_ver:
|
if remote_ver:
|
||||||
url = f'https://cdn.npm.taobao.org/dist/chromedriver/{remote_ver}chromedriver_win32.zip'
|
url = f'https://cdn.npmmirror.com/binaries/chromedriver/{remote_ver}chromedriver_win32.zip'
|
||||||
save_path = save_path or Path(__file__).parent
|
save_path = save_path or str(Path(__file__).parent)
|
||||||
result = page.download(url, save_path, file_exists='overwrite', show_msg=show_msg)
|
result = page.download(url, save_path, file_exists='overwrite', show_msg=show_msg)
|
||||||
|
|
||||||
if result[0]:
|
if result[0]:
|
||||||
|
@ -398,12 +398,14 @@ txt = ele1.ele('xpath://div/@class')
|
|||||||
查找方法能直接接收 selenium 原生定位元组进行查找,s 模式下也支持这种写法。
|
查找方法能直接接收 selenium 原生定位元组进行查找,s 模式下也支持这种写法。
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
from selenium.webdriver.common.by import By
|
||||||
|
|
||||||
# 查找 id 为 ele_id 的元素
|
# 查找 id 为 ele_id 的元素
|
||||||
loc1 = By.ID, 'ele_id'
|
loc1 = (By.ID, 'ele_id')
|
||||||
ele = page.ele(loc1)
|
ele = page.ele(loc1)
|
||||||
|
|
||||||
# 按 xpath 查找
|
# 按 xpath 查找
|
||||||
loc2 = By.XPATH, '//div[@class="ele_class"]'
|
loc2 = (By.XPATH, '//div[@class="ele_class"]'
|
||||||
ele = page.ele(loc2)
|
ele = page.ele(loc2)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ set_paths(driver_path=r"D:\chrome\chromedriver.exe",
|
|||||||
各版本 driver 下载地址:
|
各版本 driver 下载地址:
|
||||||
|
|
||||||
- 国外:https://chromedriver.chromium.org/downloads
|
- 国外:https://chromedriver.chromium.org/downloads
|
||||||
- 国内:http://npm.taobao.org/mirrors/chromedriver
|
- 国内:http://npm.taobao.org/mirrors/chromedriver/
|
||||||
|
|
||||||
# 上手示例
|
# 上手示例
|
||||||
|
|
||||||
|
2
setup.py
2
setup.py
@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="DrissionPage",
|
name="DrissionPage",
|
||||||
version="2.5.3",
|
version="2.5.4",
|
||||||
author="g1879",
|
author="g1879",
|
||||||
author_email="g1879@qq.com",
|
author_email="g1879@qq.com",
|
||||||
description="A module that integrates selenium and requests session, encapsulates common page operations.",
|
description="A module that integrates selenium and requests session, encapsulates common page operations.",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user