改进new_tab(),未完成

This commit is contained in:
g1879 2024-07-04 14:19:25 +08:00
parent 231cd0724b
commit c26a3d78b2
14 changed files with 35 additions and 28 deletions

View File

@ -9,3 +9,6 @@
2. 请附上代码和报错信息(如有)
3. DrissionPage、浏览器、python版本号是多少
4. 有什么意见建议?
请在下方写正文,不要把内容插入到上面的问题中。
---

View File

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

View File

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

View File

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

View File

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

View File

@ -170,7 +170,6 @@ class ChromiumPage(ChromiumBase):
def _on_disconnect(self):
"""浏览器退出时执行"""
print('kkk')
ChromiumPage._PAGES.pop(self._browser.id, None)
def __repr__(self):

View File

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

View File

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

View File

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

View File

@ -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秒

View File

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

View File

@ -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):
"""根据视口坐标获取绝对坐标"""

View File

@ -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属性

View File

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