4.0.4.7(+)

wait()增加scope参数;
修复上个版本出现的找浏览器路径问题;
修复在ini设置下载路径时报错问题;
修复run_async_js()报错问题;
修复reconnect()报错问题
This commit is contained in:
g1879 2024-03-02 22:43:51 +08:00
parent 8c1a549b21
commit 54bb9f138a
8 changed files with 43 additions and 34 deletions

View File

@ -14,4 +14,4 @@ from ._configs.chromium_options import ChromiumOptions
from ._configs.session_options import SessionOptions
__all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__']
__version__ = '4.0.4.6'
__version__ = '4.0.4.7'

View File

@ -193,6 +193,7 @@ class Browser(object):
def reconnect(self):
"""断开重连"""
self._driver.stop()
BrowserDriver.BROWSERS.pop(self.id)
self._driver = BrowserDriver(self.id, 'browser', self.address, self)
self.run_cdp('Target.setDiscoverTargets', discover=True)
self._driver.set_callback('Target.targetDestroyed', self._onTargetDestroyed)

View File

@ -71,6 +71,11 @@ class BrowserDriver(Driver):
def __new__(cls, tab_id: str, tab_type: str, address: str, owner: Browser): ...
def __init__(self, tab_id: str, tab_type: str, address: str, owner: Browser): ...
def __init__(self, tab_id: str, tab_type: str, address: str, owner: Browser):
"""
:rtype: object
"""
...
def get(self, url) -> Response: ...

View File

@ -1447,12 +1447,10 @@ def run_js(page_or_ele, script, as_expr, timeout, args=None):
except TimeoutError:
raise TimeoutError(f'执行js超时等待{timeout}秒)。')
except ContextLostError:
if is_page:
raise ContextLostError('页面已被刷新,请尝试等待页面加载完成再执行操作。')
else:
raise ElementLostError('原来获取到的元素对象已不在页面内。')
raise ContextLostError('页面已被刷新,请尝试等待页面加载完成再执行操作。') if is_page else ElementLostError(
'原来获取到的元素对象已不在页面内。')
if res is None and page.states.has_alert:
if not res: # _timeout=0或js激活alert时
return None
exceptionDetails = res.get('exceptionDetails')
@ -1463,7 +1461,7 @@ def run_js(page_or_ele, script, as_expr, timeout, args=None):
return parse_js_result(page, page_or_ele, res.get('result'), end_time)
except Exception:
from DrissionPage import __version__
raise RuntimeError(f'\njs结果解析错误\n内容:{res.get("result")}\n版本:{__version__}\n'
raise RuntimeError(f'\njs结果解析错误\n版本:{__version__}\n内容:{res}\njs{script}\n'
f'出现这个错误可能意味着程序有bug请把错误信息和重现方法告知作者谢谢。\n'
f'报告网站https://gitee.com/g1879/DrissionPage/issues')

View File

@ -317,23 +317,24 @@ def get_chrome_path(ini_path):
# -----------从注册表中获取--------------
from winreg import OpenKey, EnumValue, CloseKey, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, KEY_READ
txt = r'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe'
try:
key = OpenKey(HKEY_CURRENT_USER,
r'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe',
reserved=0, access=KEY_READ)
key = OpenKey(HKEY_CURRENT_USER, txt, reserved=0, access=KEY_READ)
k = EnumValue(key, 0)
CloseKey(key)
if k[1]:
return k[1]
key = OpenKey(HKEY_LOCAL_MACHINE,
r'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe',
reserved=0, access=KEY_READ)
k = EnumValue(key, 0)
CloseKey(key)
return k[1]
except FileNotFoundError:
pass
except (FileNotFoundError, OSError):
try:
key = OpenKey(HKEY_LOCAL_MACHINE, txt, reserved=0, access=KEY_READ)
k = EnumValue(key, 0)
CloseKey(key)
if k[1]:
return k[1]
except (FileNotFoundError, OSError):
pass
# -----------从系统变量中获取--------------
for path in environ.get('PATH', '').split(';'):

View File

@ -31,13 +31,7 @@ class DownloadManager(object):
self._flags = {} # {tab_id: [bool, DownloadMission]}
if self._page.download_path:
self._browser.driver.set_callback('Browser.downloadProgress', self._onDownloadProgress)
self._browser.driver.set_callback('Browser.downloadWillBegin', self._onDownloadWillBegin)
r = self._browser.run_cdp('Browser.setDownloadBehavior', downloadPath=self._page.download_path,
behavior='allowAndName', eventsEnabled=True)
if 'error' in r:
print('浏览器版本太低无法使用下载管理功能。')
self._running = True
self.set_path(self._page, self._page.download_path)
else:
self._running = False

View File

@ -18,12 +18,17 @@ class BaseWaiter(object):
"""
self._driver = page_or_ele
def __call__(self, second):
"""等待若干秒
def __call__(self, second, scope=None):
"""等待若干秒,如传入两个参数,等待时间为这两个数间的一个随机数
:param second: 秒数
:param scope: 随机数范围
:return: None
"""
sleep(second)
if scope is None:
sleep(second)
else:
from random import uniform
sleep(uniform(second, scope))
def ele_deleted(self, loc_or_ele, timeout=None, raise_err=None):
"""等待元素从DOM中删除
@ -328,12 +333,17 @@ class ElementWaiter(object):
self._page = page
self._ele = ele
def __call__(self, second):
"""等待若干秒
def __call__(self, second, scope=None):
"""等待若干秒,如传入两个参数,等待时间为这两个数间的一个随机数
:param second: 秒数
:param scope: 随机数范围
:return: None
"""
sleep(second)
if scope is None:
sleep(second)
else:
from random import uniform
sleep(uniform(second, scope))
def deleted(self, timeout=None, raise_err=None):
"""等待元素从dom删除

View File

@ -18,7 +18,7 @@ class BaseWaiter(object):
def __init__(self, page: ChromiumBase):
self._driver: ChromiumBase = ...
def __call__(self, second: float) -> None: ...
def __call__(self, second: float, scope: float = None) -> None: ...
def ele_deleted(self,
loc_or_ele: Union[str, tuple, ChromiumElement],
@ -78,7 +78,7 @@ class ElementWaiter(object):
self._ele: ChromiumElement = ...
self._page: ChromiumBase = ...
def __call__(self, second: float) -> None: ...
def __call__(self, second: float, scope: float = None) -> None: ...
def deleted(self, timeout: float = None, raise_err: bool = None) -> bool: ...