diff --git a/DrissionPage/__init__.py b/DrissionPage/__init__.py index 8faccd9..40056cf 100644 --- a/DrissionPage/__init__.py +++ b/DrissionPage/__init__.py @@ -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' diff --git a/DrissionPage/_base/browser.py b/DrissionPage/_base/browser.py index 018b84e..4ab366d 100644 --- a/DrissionPage/_base/browser.py +++ b/DrissionPage/_base/browser.py @@ -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) diff --git a/DrissionPage/_base/driver.pyi b/DrissionPage/_base/driver.pyi index a4810a2..329280b 100644 --- a/DrissionPage/_base/driver.pyi +++ b/DrissionPage/_base/driver.pyi @@ -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: ... diff --git a/DrissionPage/_elements/chromium_element.py b/DrissionPage/_elements/chromium_element.py index 9c11a49..6ba1317 100644 --- a/DrissionPage/_elements/chromium_element.py +++ b/DrissionPage/_elements/chromium_element.py @@ -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') diff --git a/DrissionPage/_functions/browser.py b/DrissionPage/_functions/browser.py index 5075945..a707c61 100644 --- a/DrissionPage/_functions/browser.py +++ b/DrissionPage/_functions/browser.py @@ -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(';'): diff --git a/DrissionPage/_units/downloader.py b/DrissionPage/_units/downloader.py index 8f7f2b5..73dc956 100644 --- a/DrissionPage/_units/downloader.py +++ b/DrissionPage/_units/downloader.py @@ -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 diff --git a/DrissionPage/_units/waiter.py b/DrissionPage/_units/waiter.py index 5398a1b..6450dec 100644 --- a/DrissionPage/_units/waiter.py +++ b/DrissionPage/_units/waiter.py @@ -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删除 diff --git a/DrissionPage/_units/waiter.pyi b/DrissionPage/_units/waiter.pyi index 92ed44b..300c176 100644 --- a/DrissionPage/_units/waiter.pyi +++ b/DrissionPage/_units/waiter.pyi @@ -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: ...