Skip to content

Commit

Permalink
4.0.4.23详见gitee
Browse files Browse the repository at this point in the history
  • Loading branch information
g1879 authored May 28, 2024
1 parent c522faf commit 378d27f
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
17 changes: 17 additions & 0 deletions DrissionPage/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding:utf-8 -*-
"""
@Author : g1879
@Contact : [email protected]
@Copyright: (c) 2024 by g1879, Inc. All Rights Reserved.
@License : BSD 3-Clause.
"""
from ._pages.chromium_page import ChromiumPage
from ._pages.session_page import SessionPage
from ._pages.web_page import WebPage

# 启动配置类
from ._configs.chromium_options import ChromiumOptions
from ._configs.session_options import SessionOptions

__all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__']
__version__ = '4.0.4.23'
51 changes: 51 additions & 0 deletions DrissionPage/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding:utf-8 -*-
"""
@Author : g1879
@Contact : [email protected]
@Copyright: (c) 2024 by g1879, Inc. All Rights Reserved.
@License : BSD 3-Clause.
"""
from ._elements.session_element import make_session_ele
from ._functions.by import By
from ._functions.keys import Keys
from ._functions.settings import Settings
from ._functions.tools import wait_until, configs_to_here
from ._functions.web import get_blob, tree
from ._pages.chromium_page import ChromiumPage
from ._units.actions import Actions

__all__ = ['make_session_ele', 'Actions', 'Keys', 'By', 'Settings', 'wait_until', 'configs_to_here', 'get_blob',
'tree', 'from_selenium', 'from_playwright']


def from_selenium(driver):
"""从selenium的WebDriver对象生成ChromiumPage对象"""
address, port = driver.caps.get('goog:chromeOptions', {}).get('debuggerAddress', ':').split(':')
if not address:
raise RuntimeError('获取失败。')
return ChromiumPage(f'{address}:{port}')


def from_playwright(page_or_browser):
"""从playwright的Page或Browser对象生成ChromiumPage对象"""
if hasattr(page_or_browser, 'context'):
page_or_browser = page_or_browser.context.browser
try:
processes = page_or_browser.new_browser_cdp_session().send('SystemInfo.getProcessInfo')['processInfo']
for process in processes:
if process['type'] == 'browser':
pid = process['id']
break
else:
raise RuntimeError('获取失败。')
except:
raise RuntimeError('获取失败。')

from psutil import net_connections
for con_info in net_connections():
if con_info.pid == pid:
port = con_info.laddr.port
break
else:
raise RuntimeError('获取失败。')
return ChromiumPage(f'127.0.0.1:{port}')
95 changes: 95 additions & 0 deletions DrissionPage/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# -*- coding:utf-8 -*-
"""
@Author : g1879
@Contact : [email protected]
@Copyright: (c) 2024 by g1879, Inc. All Rights Reserved.
@License : BSD 3-Clause.
"""


class BaseError(Exception):
_info = None

def __init__(self, ErrorInfo=None):
self._info = ErrorInfo or self._info

def __str__(self):
return self._info


class ElementNotFoundError(BaseError):
_info = '\n没有找到元素。'

def __init__(self, ErrorInfo=None, method=None, arguments=None):
super().__init__(ErrorInfo=ErrorInfo)
self.method = method
self.arguments = arguments

def __str__(self):
method = f'\nmethod: {self.method}' if self.method else ''
arguments = f'\nargs: {self.arguments}' if self.arguments else ''
return f'{self._info}{method}{arguments}'


class AlertExistsError(BaseError):
_info = '存在未处理的提示框。'


class ContextLostError(BaseError):
_info = '页面被刷新,请操作前尝试等待页面刷新或加载完成。'


class ElementLostError(BaseError):
_info = '元素对象已失效。可能是页面整体刷新,或js局部刷新把元素替换或去除了。'


class CDPError(BaseError):
_info = '方法调用错误。'


class PageDisconnectedError(BaseError):
_info = '与页面的连接已断开。'


class JavaScriptError(BaseError):
_info = 'JavaScript运行错误。'


class NoRectError(BaseError):
_info = '该元素没有位置及大小。'


class BrowserConnectError(BaseError):
_info = '浏览器连接失败。'


class NoResourceError(BaseError):
_info = '该元素无可保存的内容或保存失败。'


class CanNotClickError(BaseError):
_info = '该元素无法滚动到视口或被遮挡,无法点击。'


class GetDocumentError(BaseError):
_info = '获取文档失败。'


class WaitTimeoutError(BaseError):
_info = '等待失败。'


class WrongURLError(BaseError):
_info = '无效的url。'


class StorageError(BaseError):
_info = '无法操作当前存储数据。'


class CookieFormatError(BaseError):
_info = 'cookie格式不正确。'


class TargetNotFoundError(BaseError):
_info = '找不到指定页面。'

0 comments on commit 378d27f

Please sign in to comment.