From c26a3d78b2ee3aba23186f6823d045d04f19bacf Mon Sep 17 00:00:00 2001 From: g1879 Date: Thu, 4 Jul 2024 14:19:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Bnew=5Ftab()=EF=BC=8C=E6=9C=AA?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitee/ISSUE_TEMPLATE.zh-CN.md | 3 +++ DrissionPage/_base/browser.py | 11 +++++++++-- DrissionPage/_base/browser.pyi | 2 +- DrissionPage/_pages/chromium_base.py | 5 +++-- DrissionPage/_pages/chromium_base.pyi | 2 +- DrissionPage/_pages/chromium_page.py | 1 - DrissionPage/_pages/chromium_page.pyi | 1 - DrissionPage/_pages/chromium_tab.pyi | 2 +- DrissionPage/_pages/session_page.pyi | 2 +- DrissionPage/_pages/web_page.py | 2 +- DrissionPage/_units/clicker.py | 8 ++++---- DrissionPage/_units/rect.py | 6 ++---- DrissionPage/_units/setter.py | 12 ++++++------ DrissionPage/_units/states.py | 6 +++--- 14 files changed, 35 insertions(+), 28 deletions(-) diff --git a/.gitee/ISSUE_TEMPLATE.zh-CN.md b/.gitee/ISSUE_TEMPLATE.zh-CN.md index c6e48e4..69c29c4 100644 --- a/.gitee/ISSUE_TEMPLATE.zh-CN.md +++ b/.gitee/ISSUE_TEMPLATE.zh-CN.md @@ -9,3 +9,6 @@ 2. 请附上代码和报错信息(如有) 3. DrissionPage、浏览器、python版本号是多少? 4. 有什么意见建议? + +请在下方写正文,不要把内容插入到上面的问题中。 +--- \ No newline at end of file diff --git a/DrissionPage/_base/browser.py b/DrissionPage/_base/browser.py index 9d232f0..e5bc502 100644 --- a/DrissionPage/_base/browser.py +++ b/DrissionPage/_base/browser.py @@ -26,7 +26,7 @@ from .._pages.chromium_tab import ChromiumTab, MixTab from .._units.downloader import DownloadManager from .._units.setter import BrowserSetter from .._units.waiter import BrowserWaiter -from ..errors import BrowserConnectError +from ..errors import BrowserConnectError, CDPError from ..errors import PageDisconnectedError __ERROR__ = 'error' @@ -219,7 +219,14 @@ class Browser(object): if tab: kwargs['browserContextId'] = tab - tab = self._run_cdp('Target.createTarget', **kwargs)['targetId'] + try: + tab = self._run_cdp('Target.createTarget', **kwargs)['targetId'] + except CDPError: + url = url or 'https://#' + tab = self.get_tab().add_ele(('a', {'href': url, + 'target': '_blank'})).click.for_new_tab(by_js=True) + return tab + while tab not in self._drivers: sleep(.1) tab = obj(self, tab) diff --git a/DrissionPage/_base/browser.pyi b/DrissionPage/_base/browser.pyi index 82b5611..1da87c4 100644 --- a/DrissionPage/_base/browser.pyi +++ b/DrissionPage/_base/browser.pyi @@ -11,7 +11,7 @@ from typing import List, Optional, Set, Dict, Union, Tuple from .driver import BrowserDriver, Driver from .._configs.chromium_options import ChromiumOptions from .._configs.session_options import SessionOptions -from .._functions.web import CookiesList +from .._functions.cookies import CookiesList from .._pages.chromium_base import Timeout from .._pages.chromium_tab import ChromiumTab, MixTab from .._units.downloader import DownloadManager diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 27b2e3c..01e908a 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -151,7 +151,7 @@ class ChromiumBase(BasePage): timeout = end_time - perf_counter() timeout = 1 if timeout <= 1 else timeout self._root_id = self._run_cdp('DOM.resolveNode', backendNodeId=b_id, - _timeout=timeout)['object']['objectId'] + _timeout=timeout)['object']['objectId'] result = True break @@ -842,7 +842,7 @@ class ChromiumBase(BasePage): :return: 添加的脚本的id """ js_id = self._run_cdp('Page.addScriptToEvaluateOnNewDocument', source=script, - includeCommandLineAPI=True)['identifier'] + includeCommandLineAPI=True)['identifier'] self._init_jss.append(js_id) return js_id @@ -1185,6 +1185,7 @@ def close_privacy_dialog(page, tid): :return: None """ try: + print('ooo') driver = page.browser._get_driver(tid) driver.run('Runtime.enable') driver.run('DOM.enable') diff --git a/DrissionPage/_pages/chromium_base.pyi b/DrissionPage/_pages/chromium_base.pyi index 6653a38..4d3f739 100644 --- a/DrissionPage/_pages/chromium_base.pyi +++ b/DrissionPage/_pages/chromium_base.pyi @@ -14,8 +14,8 @@ from .._base.browser import Browser from .._base.driver import Driver from .._elements.chromium_element import ChromiumElement from .._elements.session_element import SessionElement +from .._functions.cookies import CookiesList from .._functions.elements import SessionElementsList, ChromiumElementsList -from .._functions.web import CookiesList from .._pages.chromium_frame import ChromiumFrame from .._pages.chromium_page import ChromiumPage from .._units.actions import Actions diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index a8bfda8..3aca524 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -170,7 +170,6 @@ class ChromiumPage(ChromiumBase): def _on_disconnect(self): """浏览器退出时执行""" - print('kkk') ChromiumPage._PAGES.pop(self._browser.id, None) def __repr__(self): diff --git a/DrissionPage/_pages/chromium_page.pyi b/DrissionPage/_pages/chromium_page.pyi index 335a604..77e4924 100644 --- a/DrissionPage/_pages/chromium_page.pyi +++ b/DrissionPage/_pages/chromium_page.pyi @@ -6,7 +6,6 @@ @License : BSD 3-Clause. """ from pathlib import Path -from threading import Lock from typing import Union, Tuple, List, Optional from .._base.browser import Browser diff --git a/DrissionPage/_pages/chromium_tab.pyi b/DrissionPage/_pages/chromium_tab.pyi index 072de9f..288fdfe 100644 --- a/DrissionPage/_pages/chromium_tab.pyi +++ b/DrissionPage/_pages/chromium_tab.pyi @@ -16,8 +16,8 @@ from .session_page import SessionPage from .._base.browser import Browser from .._elements.chromium_element import ChromiumElement from .._elements.session_element import SessionElement +from .._functions.cookies import CookiesList from .._functions.elements import SessionElementsList, ChromiumElementsList -from .._functions.web import CookiesList from .._units.rect import TabRect from .._units.setter import TabSetter, WebPageTabSetter from .._units.waiter import TabWaiter diff --git a/DrissionPage/_pages/session_page.pyi b/DrissionPage/_pages/session_page.pyi index 385ac2b..2324ac0 100644 --- a/DrissionPage/_pages/session_page.pyi +++ b/DrissionPage/_pages/session_page.pyi @@ -14,8 +14,8 @@ from requests.structures import CaseInsensitiveDict from .._base.base import BasePage from .._configs.session_options import SessionOptions from .._elements.session_element import SessionElement +from .._functions.cookies import CookiesList from .._functions.elements import SessionElementsList -from .._functions.web import CookiesList from .._units.setter import SessionPageSetter diff --git a/DrissionPage/_pages/web_page.py b/DrissionPage/_pages/web_page.py index 90f3790..c79ae8d 100644 --- a/DrissionPage/_pages/web_page.py +++ b/DrissionPage/_pages/web_page.py @@ -25,7 +25,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): """ return super().__new__(cls, chromium_options) - def __init__(self, mode='d', timeout=None, chromium_options=None, session_or_options=None, driver_or_options=None): + def __init__(self, mode='d', timeout=None, chromium_options=None, session_or_options=None): """初始化函数 :param mode: 'd' 或 's',即driver模式和session模式 :param timeout: 超时时间(秒),d模式时为寻找元素时间,s模式时为连接时间,默认10秒 diff --git a/DrissionPage/_units/clicker.py b/DrissionPage/_units/clicker.py index 12e85f3..f18014a 100644 --- a/DrissionPage/_units/clicker.py +++ b/DrissionPage/_units/clicker.py @@ -88,7 +88,7 @@ class Clicker(object): y = rect[0][0] + 3 try: r = self._ele.owner._run_cdp('DOM.getNodeForLocation', x=int(x), y=int(y), - includeUserAgentShadowDOM=True, ignorePointerEventsNone=True) + includeUserAgentShadowDOM=True, ignorePointerEventsNone=True) if r['backendNodeId'] != self._ele._backend_id: vx, vy = self._ele.rect.viewport_midpoint else: @@ -166,7 +166,7 @@ class Clicker(object): elif not self._ele.tab._browser._dl_mgr._running: self._ele.tab._browser.set.download_path('.') - obj = self._ele.tab._browser if new_tab else self._ele.owner.tab + obj = self._ele.tab._browser if new_tab else self._ele.owner._tab if rename or suffix: obj.set.download_file_name(rename, suffix) @@ -205,6 +205,6 @@ class Clicker(object): :return: None """ self._ele.owner._run_cdp('Input.dispatchMouseEvent', type='mousePressed', x=client_x, - y=client_y, button=button, clickCount=count, _ignore=AlertExistsError) + y=client_y, button=button, clickCount=count, _ignore=AlertExistsError) self._ele.owner._run_cdp('Input.dispatchMouseEvent', type='mouseReleased', x=client_x, - y=client_y, button=button, _ignore=AlertExistsError) + y=client_y, button=button, _ignore=AlertExistsError) diff --git a/DrissionPage/_units/rect.py b/DrissionPage/_units/rect.py index 2d8601e..68e8683 100644 --- a/DrissionPage/_units/rect.py +++ b/DrissionPage/_units/rect.py @@ -33,7 +33,7 @@ class ElementRect(object): def size(self): """返回元素大小,格式(宽, 高)""" border = self._ele.owner._run_cdp('DOM.getBoxModel', backendNodeId=self._ele._backend_id, - nodeId=self._ele._node_id, objectId=self._ele._obj_id)['model']['border'] + nodeId=self._ele._node_id, objectId=self._ele._obj_id)['model']['border'] return border[2] - border[0], border[5] - border[1] @property @@ -101,9 +101,7 @@ class ElementRect(object): :param quad: 方框类型,margin border padding :return: 四个角坐标 """ - return self._ele.owner._run_cdp('DOM.getBoxModel', backendNodeId=self._ele._backend_id, - # nodeId=self._ele._node_id, objectId=self._ele._obj_id - )['model'][quad] + return self._ele.owner._run_cdp('DOM.getBoxModel', backendNodeId=self._ele._backend_id)['model'][quad] def _get_page_coord(self, x, y): """根据视口坐标获取绝对坐标""" diff --git a/DrissionPage/_units/setter.py b/DrissionPage/_units/setter.py index 04c4660..46353d0 100644 --- a/DrissionPage/_units/setter.py +++ b/DrissionPage/_units/setter.py @@ -173,10 +173,10 @@ class ChromiumBaseSetter(BrowserBaseSetter): i = self._owner._run_cdp('Storage.getStorageKeyForFrame', frameId=self._owner._frame_id)['storageKey'] if value is False: self._owner._run_cdp('DOMStorage.removeDOMStorageItem', - storageId={'storageKey': i, 'isLocalStorage': False}, key=item) + storageId={'storageKey': i, 'isLocalStorage': False}, key=item) else: self._owner._run_cdp('DOMStorage.setDOMStorageItem', storageId={'storageKey': i, 'isLocalStorage': False}, - key=item, value=value) + key=item, value=value) self._owner._run_cdp_loaded('DOMStorage.disable') def local_storage(self, item, value): @@ -189,10 +189,10 @@ class ChromiumBaseSetter(BrowserBaseSetter): i = self._owner._run_cdp('Storage.getStorageKeyForFrame', frameId=self._owner._frame_id)['storageKey'] if value is False: self._owner._run_cdp('DOMStorage.removeDOMStorageItem', - storageId={'storageKey': i, 'isLocalStorage': True}, key=item) + storageId={'storageKey': i, 'isLocalStorage': True}, key=item) else: self._owner._run_cdp('DOMStorage.setDOMStorageItem', storageId={'storageKey': i, 'isLocalStorage': True}, - key=item, value=value) + key=item, value=value) self._owner._run_cdp_loaded('DOMStorage.disable') def upload_files(self, files): @@ -528,11 +528,11 @@ class ChromiumElementSetter(object): """ try: self._ele.owner._run_cdp('DOM.setAttributeValue', - nodeId=self._ele._node_id, name=name, value=str(value)) + nodeId=self._ele._node_id, name=name, value=str(value)) except ElementLostError: self._ele._refresh_id() self._ele.owner._run_cdp('DOM.setAttributeValue', - nodeId=self._ele._node_id, name=name, value=str(value)) + nodeId=self._ele._node_id, name=name, value=str(value)) def property(self, name, value): """设置元素property属性 diff --git a/DrissionPage/_units/states.py b/DrissionPage/_units/states.py index 4a75c1c..0a664b1 100644 --- a/DrissionPage/_units/states.py +++ b/DrissionPage/_units/states.py @@ -43,7 +43,7 @@ class ElementStates(object): """返回元素是否仍在DOM中""" try: return self._ele.owner._run_cdp('DOM.describeNode', - backendNodeId=self._ele._backend_id)['node']['nodeId'] != 0 + backendNodeId=self._ele._backend_id)['node']['nodeId'] != 0 except ElementLostError: return False @@ -102,7 +102,7 @@ class ShadowRootStates(object): """返回元素是否仍在DOM中""" try: return self._ele.owner._run_cdp('DOM.describeNode', - backendNodeId=self._ele._backend_id)['node']['nodeId'] != 0 + backendNodeId=self._ele._backend_id)['node']['nodeId'] != 0 except ElementLostError: return False @@ -158,7 +158,7 @@ class FrameStates(object): """返回frame元素是否可用,且里面仍挂载有frame""" try: node = self._frame._target_page._run_cdp('DOM.describeNode', - backendNodeId=self._frame._frame_ele._backend_id)['node'] + backendNodeId=self._frame._frame_ele._backend_id)['node'] except (ElementLostError, PageDisconnectedError): return False return 'frameId' in node