From 4cf63e9b08cbfd0945964ae4b49a98554b78b787 Mon Sep 17 00:00:00 2001 From: g1879 Date: Sun, 7 Jul 2024 11:10:24 +0800 Subject: [PATCH] =?UTF-8?q?WebPage=E6=94=B9=E5=90=8D=E4=B8=BAMixPage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/__init__.py | 7 +- DrissionPage/__init__.pyi | 5 +- DrissionPage/_base/base.py | 6 - DrissionPage/_base/base.pyi | 9 +- DrissionPage/_base/browser.py | 2 +- DrissionPage/_base/browser.pyi | 2 +- DrissionPage/_elements/chromium_element.py | 6 +- DrissionPage/_elements/chromium_element.pyi | 10 +- DrissionPage/_elements/session_element.py | 2 +- DrissionPage/_functions/web.pyi | 2 +- DrissionPage/_pages/chromium_base.py | 9 +- DrissionPage/_pages/chromium_base.pyi | 5 +- DrissionPage/_pages/chromium_frame.py | 2 +- DrissionPage/_pages/chromium_frame.pyi | 2 +- DrissionPage/_pages/chromium_page.pyi | 2 +- .../_pages/{web_page.py => mix_page.py} | 16 +- .../_pages/{web_page.pyi => mix_page.pyi} | 12 +- DrissionPage/_pages/session_page.py | 6 + DrissionPage/_pages/session_page.pyi | 3 + .../_pages/{chromium_tab.py => tabs.py} | 9 +- .../_pages/{chromium_tab.pyi => tabs.pyi} | 4 +- DrissionPage/_units/clicker.pyi | 2 +- DrissionPage/_units/cookies_setter.py | 2 +- DrissionPage/_units/cookies_setter.pyi | 8 +- DrissionPage/_units/rect.pyi | 6 +- DrissionPage/_units/setter.py | 299 +++++++++--------- DrissionPage/_units/setter.pyi | 189 ++++++----- DrissionPage/items.py | 2 +- 28 files changed, 319 insertions(+), 310 deletions(-) rename DrissionPage/_pages/{web_page.py => mix_page.py} (97%) rename DrissionPage/_pages/{web_page.pyi => mix_page.pyi} (96%) rename DrissionPage/_pages/{chromium_tab.py => tabs.py} (98%) rename DrissionPage/_pages/{chromium_tab.pyi => tabs.pyi} (98%) diff --git a/DrissionPage/__init__.py b/DrissionPage/__init__.py index b29f1e8..83e7a10 100644 --- a/DrissionPage/__init__.py +++ b/DrissionPage/__init__.py @@ -8,9 +8,10 @@ from ._base.browser import Chromium from ._configs.chromium_options import ChromiumOptions from ._configs.session_options import SessionOptions -# 即将废弃 -from ._pages.chromium_page import ChromiumPage from ._pages.session_page import SessionPage -from ._pages.web_page import WebPage + +from ._pages.chromium_page import ChromiumPage +from ._pages.mix_page import MixPage +from ._pages.mix_page import MixPage as WebPage __version__ = '4.1.0.0b1' diff --git a/DrissionPage/__init__.pyi b/DrissionPage/__init__.pyi index 055e4fd..5fc228d 100644 --- a/DrissionPage/__init__.pyi +++ b/DrissionPage/__init__.pyi @@ -11,7 +11,8 @@ from ._configs.session_options import SessionOptions from ._pages.session_page import SessionPage from ._pages.chromium_page import ChromiumPage -from ._pages.web_page import WebPage +from ._pages.mix_page import MixPage +from ._pages.mix_page import MixPage as WebPage -__all__ = ['WebPage', 'ChromiumPage', 'Chromium', 'ChromiumOptions', 'SessionOptions', 'SessionPage', '__version__'] +__all__ = ['MixPage', 'WebPage', 'ChromiumPage', 'Chromium', 'ChromiumOptions', 'SessionOptions', 'SessionPage', '__version__'] __version__: str = ... diff --git a/DrissionPage/_base/base.py b/DrissionPage/_base/base.py index d4f8d78..b0e69ca 100644 --- a/DrissionPage/_base/base.py +++ b/DrissionPage/_base/base.py @@ -346,7 +346,6 @@ class BasePage(BaseParser): def __init__(self): """初始化函数""" self._url = None - self._timeout = 10 self._url_available = None self.retry_times = 3 self.retry_interval = 2 @@ -362,11 +361,6 @@ class BasePage(BaseParser): ele = self._ele('xpath://title', raise_err=False, method='title') return ele.text if ele else None - @property - def timeout(self): - """返回查找元素时等待的秒数""" - return self._timeout - @property def url_available(self): """返回当前访问的url有效性""" diff --git a/DrissionPage/_base/base.pyi b/DrissionPage/_base/base.pyi index 27fcb1f..39cd123 100644 --- a/DrissionPage/_base/base.pyi +++ b/DrissionPage/_base/base.pyi @@ -15,7 +15,7 @@ from .._elements.session_element import SessionElement from .._functions.elements import SessionElementsList from .._pages.chromium_page import ChromiumPage from .._pages.session_page import SessionPage -from .._pages.web_page import WebPage +from .._pages.mix_page import MixPage class BaseParser(object): @@ -59,7 +59,6 @@ class BaseElement(BaseParser): def __init__(self, owner: BasePage = None): self.owner: BasePage = ... - # self.page: Union[ChromiumPage, SessionPage, WebPage] = ... # ----------------以下属性或方法由后代实现---------------- @property @@ -200,19 +199,15 @@ class BasePage(BaseParser): self._url_available: bool = ... self.retry_times: int = ... self.retry_interval: float = ... - self._timeout: float = ... self._download_path: str = ... self._DownloadKit: DownloadKit = ... self._none_ele_return_value: bool = ... self._none_ele_value: Any = ... - self._page: Union[ChromiumPage, SessionPage, WebPage] = ... + self._page: Union[ChromiumPage, SessionPage, MixPage] = ... @property def title(self) -> Union[str, None]: ... - @property - def timeout(self) -> float: ... - @property def url_available(self) -> bool: ... diff --git a/DrissionPage/_base/browser.py b/DrissionPage/_base/browser.py index f3c2a86..fb6e940 100644 --- a/DrissionPage/_base/browser.py +++ b/DrissionPage/_base/browser.py @@ -22,7 +22,7 @@ from .._functions.settings import Settings from .._functions.tools import PortFinder from .._functions.tools import raise_error from .._pages.chromium_base import Timeout -from .._pages.chromium_tab import ChromiumTab, MixTab +from .._pages.tabs import ChromiumTab, MixTab from .._units.downloader import DownloadManager from .._units.setter import BrowserSetter from .._units.waiter import BrowserWaiter diff --git a/DrissionPage/_base/browser.pyi b/DrissionPage/_base/browser.pyi index d772f9e..aa3978c 100644 --- a/DrissionPage/_base/browser.pyi +++ b/DrissionPage/_base/browser.pyi @@ -13,7 +13,7 @@ from .._configs.chromium_options import ChromiumOptions from .._configs.session_options import SessionOptions from .._functions.cookies import CookiesList from .._pages.chromium_base import Timeout -from .._pages.chromium_tab import ChromiumTab, MixTab +from .._pages.tabs import ChromiumTab, MixTab from .._units.downloader import DownloadManager from .._units.setter import BrowserSetter from .._units.waiter import BrowserWaiter diff --git a/DrissionPage/_elements/chromium_element.py b/DrissionPage/_elements/chromium_element.py index 091593f..ffbc68d 100644 --- a/DrissionPage/_elements/chromium_element.py +++ b/DrissionPage/_elements/chromium_element.py @@ -584,7 +584,7 @@ class ChromiumElement(DrissionElement): :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param timeout: 查找元素超时时间(秒) :param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有 - :param relative: WebPage用的表示是否相对定位的参数 + :param relative: MixTab用的表示是否相对定位的参数 :param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置 :return: ChromiumElement对象或文本、属性或其组成的列表 """ @@ -1174,7 +1174,7 @@ class ShadowRoot(BaseElement): :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param timeout: 查找元素超时时间(秒) :param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有 - :param relative: WebPage用的表示是否相对定位的参数 + :param relative: MixTab用的表示是否相对定位的参数 :param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置 :return: ChromiumElement对象或其组成的列表 """ @@ -1250,7 +1250,7 @@ def find_in_chromium_ele(ele, locator, index=1, timeout=None, relative=True): :param locator: 元素定位元组 :param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有 :param timeout: 查找元素超时时间(秒) - :param relative: WebPage用于标记是否相对定位使用 + :param relative: MixTab用于标记是否相对定位使用 :return: 返回ChromiumElement元素或它们组成的列表 """ # ---------------处理定位符--------------- diff --git a/DrissionPage/_elements/chromium_element.pyi b/DrissionPage/_elements/chromium_element.pyi index 648c68e..cec49db 100644 --- a/DrissionPage/_elements/chromium_element.pyi +++ b/DrissionPage/_elements/chromium_element.pyi @@ -14,8 +14,8 @@ from .._functions.elements import SessionElementsList, ChromiumElementsList from .._pages.chromium_base import ChromiumBase from .._pages.chromium_frame import ChromiumFrame from .._pages.chromium_page import ChromiumPage -from .._pages.chromium_tab import ChromiumTab -from .._pages.web_page import WebPage +from .._pages.tabs import ChromiumTab +from .._pages.mix_page import MixPage from .._units.clicker import Clicker from .._units.rect import ElementRect from .._units.scroller import ElementScroller @@ -31,9 +31,8 @@ class ChromiumElement(DrissionElement): def __init__(self, owner: ChromiumBase, node_id: int = None, obj_id: str = None, backend_id: int = None): self._tag: str = ... - # self.page: Union[ChromiumPage, WebPage] = ... self.owner: ChromiumBase = ... - self.page: Union[ChromiumPage, WebPage] = ... + self.page: Union[ChromiumPage, MixPage] = ... self.tab: Union[ChromiumPage, ChromiumTab] = ... self._node_id: int = ... self._obj_id: str = ... @@ -268,7 +267,6 @@ class ChromiumElement(DrissionElement): class ShadowRoot(BaseElement): def __init__(self, parent_ele: ChromiumElement, obj_id: str = None, backend_id: int = None): - # self.page: Union[ChromiumPage, WebPage] = ... self.owner: ChromiumBase = ... self.tab: Union[ChromiumPage, ChromiumTab] = ... self._obj_id: str = ... @@ -379,7 +377,7 @@ def find_by_css(ele: ChromiumElement, timeout: float) -> Union[ChromiumElement, List[ChromiumElement],]: ... -def make_chromium_eles(page: Union[ChromiumBase, ChromiumPage, WebPage, ChromiumTab, ChromiumFrame], +def make_chromium_eles(page: Union[ChromiumBase, ChromiumPage, MixPage, ChromiumTab, ChromiumFrame], _ids: Union[tuple, list, str, int], index: Optional[int] = 1, is_obj_id: bool = True, diff --git a/DrissionPage/_elements/session_element.py b/DrissionPage/_elements/session_element.py index f0d35a1..d746dd5 100644 --- a/DrissionPage/_elements/session_element.py +++ b/DrissionPage/_elements/session_element.py @@ -260,7 +260,7 @@ class SessionElement(DrissionElement): :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param timeout: 不起实际作用,用于和父类对应 :param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有 - :param relative: WebPage用的表示是否相对定位的参数 + :param relative: MixTab用的表示是否相对定位的参数 :param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置 :return: SessionElement对象 """ diff --git a/DrissionPage/_functions/web.pyi b/DrissionPage/_functions/web.pyi index 2f5e702..c7a40db 100644 --- a/DrissionPage/_functions/web.pyi +++ b/DrissionPage/_functions/web.pyi @@ -12,7 +12,7 @@ from .._base.base import DrissionElement, BaseParser from .._elements.chromium_element import ChromiumElement from .._pages.chromium_base import ChromiumBase from .._pages.chromium_page import ChromiumPage -from .._pages.chromium_tab import ChromiumTab +from .._pages.tabs import ChromiumTab def get_ele_txt(e: DrissionElement) -> str: ... diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 6aea14a..98107c5 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -313,6 +313,11 @@ class ChromiumBase(BasePage): self._rect = TabRect(self) return self._rect + @property + def timeout(self): + """返回timeout设置""" + return self._timeouts.base + @property def timeouts(self): """返回timeouts设置""" @@ -343,7 +348,7 @@ class ChromiumBase(BasePage): @property def _browser_url(self): - """用于被WebPage覆盖""" + """用于被MixTab覆盖""" return self.url @property @@ -556,7 +561,7 @@ class ChromiumBase(BasePage): :param locator: 定位符或元素对象 :param timeout: 查找超时时间(秒) :param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有 - :param relative: WebPage用的表示是否相对定位的参数 + :param relative: MixTab用的表示是否相对定位的参数 :param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置 :return: ChromiumElement对象或元素对象组成的列表 """ diff --git a/DrissionPage/_pages/chromium_base.pyi b/DrissionPage/_pages/chromium_base.pyi index 3f946c1..2bc1a8d 100644 --- a/DrissionPage/_pages/chromium_base.pyi +++ b/DrissionPage/_pages/chromium_base.pyi @@ -8,7 +8,7 @@ from pathlib import Path from typing import Union, Tuple, List, Any, Optional, Literal -from .chromium_tab import ChromiumTab, MixTab +from .tabs import ChromiumTab, MixTab from .._base.base import BasePage from .._base.browser import Chromium from .._base.driver import Driver @@ -143,6 +143,9 @@ class ChromiumBase(BasePage): @property def rect(self) -> TabRect: ... + @property + def timeout(self) -> float: ... + @property def timeouts(self) -> Timeout: ... diff --git a/DrissionPage/_pages/chromium_frame.py b/DrissionPage/_pages/chromium_frame.py index 8cf684b..695934e 100644 --- a/DrissionPage/_pages/chromium_frame.py +++ b/DrissionPage/_pages/chromium_frame.py @@ -555,7 +555,7 @@ class ChromiumFrame(ChromiumBase): :param locator: 定位符或元素对象 :param timeout: 查找超时时间(秒) :param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有 - :param relative: WebPage用的表示是否相对定位的参数 + :param relative: MixTab用的表示是否相对定位的参数 :param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置 :return: ChromiumElement对象 """ diff --git a/DrissionPage/_pages/chromium_frame.pyi b/DrissionPage/_pages/chromium_frame.pyi index a23ed33..61b44ad 100644 --- a/DrissionPage/_pages/chromium_frame.pyi +++ b/DrissionPage/_pages/chromium_frame.pyi @@ -9,7 +9,7 @@ from pathlib import Path from typing import Union, Tuple, List, Any, Optional from .chromium_base import ChromiumBase -from .chromium_tab import ChromiumTab, MixTab +from .tabs import ChromiumTab, MixTab from .._elements.chromium_element import ChromiumElement from .._functions.elements import ChromiumElementsList from .._units.listener import FrameListener diff --git a/DrissionPage/_pages/chromium_page.pyi b/DrissionPage/_pages/chromium_page.pyi index 241a30b..22c31fc 100644 --- a/DrissionPage/_pages/chromium_page.pyi +++ b/DrissionPage/_pages/chromium_page.pyi @@ -11,7 +11,7 @@ from typing import Union, Tuple, List, Optional from .._base.browser import Chromium from .._configs.chromium_options import ChromiumOptions from .._pages.chromium_base import ChromiumBase -from .._pages.chromium_tab import ChromiumTab +from .._pages.tabs import ChromiumTab from .._units.rect import TabRect from .._units.setter import ChromiumPageSetter from .._units.waiter import PageWaiter diff --git a/DrissionPage/_pages/web_page.py b/DrissionPage/_pages/mix_page.py similarity index 97% rename from DrissionPage/_pages/web_page.py rename to DrissionPage/_pages/mix_page.py index c79ae8d..0afbaed 100644 --- a/DrissionPage/_pages/web_page.py +++ b/DrissionPage/_pages/mix_page.py @@ -10,10 +10,10 @@ from .session_page import SessionPage from .._base.base import BasePage from .._configs.chromium_options import ChromiumOptions from .._functions.cookies import set_session_cookies, set_tab_cookies -from .._units.setter import WebPageSetter +from .._units.setter import MixPageSetter -class WebPage(SessionPage, ChromiumPage, BasePage): +class MixPage(SessionPage, ChromiumPage, BasePage): """整合浏览器和request的页面类""" def __new__(cls, mode='d', timeout=None, chromium_options=None, session_or_options=None): @@ -46,7 +46,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): chromium_options = ChromiumOptions(read_file=chromium_options) chromium_options.set_timeouts(base=self._timeout).set_paths(download_path=self.download_path) super(SessionPage, self).__init__(addr_or_opts=chromium_options, timeout=timeout) - self._type = 'WebPage' + self._type = 'MixPage' self.change_mode(self._mode, go=False, copy_cookies=False) def __call__(self, locator, index=1, timeout=None): @@ -66,7 +66,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def set(self): """返回用于设置的对象""" if self._set is None: - self._set = WebPageSetter(self) + self._set = MixPageSetter(self) return self._set @property @@ -147,7 +147,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): @property def timeout(self): """返回通用timeout设置""" - return self.timeouts.base + return super()._timeout if self._mode == 's' else self.timeouts.base def get(self, url, show_errmsg=False, retry=None, interval=None, timeout=None, **kwargs): """跳转到一个url @@ -304,7 +304,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): :param url: 要匹配url的文本,模糊匹配,为None则匹配所有 :param tab_type: tab类型,可用列表输入多个,如 'page', 'iframe' 等,为None则匹配所有 :param as_id: 是否返回标签页id而不是标签页对象 - :return: WebPageTab对象 + :return: MixTab对象 """ return self.browser._get_tab(id_or_num=id_or_num, title=title, url=url, tab_type=tab_type, mix=True, as_id=as_id) @@ -366,7 +366,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): :param locator: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 :param timeout: 查找元素超时时间(秒),d模式专用 :param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有 - :param relative: WebPage用的表示是否相对定位的参数 + :param relative: MixTab用的表示是否相对定位的参数 :param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置 :return: 元素对象或属性、文本节点文本 """ @@ -392,4 +392,4 @@ class WebPage(SessionPage, ChromiumPage, BasePage): self._has_driver = None def __repr__(self): - return f'' + return f'' diff --git a/DrissionPage/_pages/web_page.pyi b/DrissionPage/_pages/mix_page.pyi similarity index 96% rename from DrissionPage/_pages/web_page.pyi rename to DrissionPage/_pages/mix_page.pyi index 1b0e28c..7d335da 100644 --- a/DrissionPage/_pages/web_page.pyi +++ b/DrissionPage/_pages/mix_page.pyi @@ -11,7 +11,7 @@ from requests import Session, Response from .chromium_frame import ChromiumFrame from .chromium_page import ChromiumPage -from .chromium_tab import MixTab +from .tabs import MixTab from .session_page import SessionPage from .._base.base import BasePage from .._base.driver import Driver @@ -20,10 +20,10 @@ from .._configs.session_options import SessionOptions from .._elements.chromium_element import ChromiumElement from .._elements.session_element import SessionElement from .._functions.elements import SessionElementsList, ChromiumElementsList -from .._units.setter import WebPageSetter +from .._units.setter import MixPageSetter -class WebPage(SessionPage, ChromiumPage, BasePage): +class MixPage(SessionPage, ChromiumPage, BasePage): def __init__(self, mode: str = 'd', @@ -31,7 +31,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): chromium_options: Union[ChromiumOptions, bool] = None, session_or_options: Union[Session, SessionOptions, bool] = None) -> None: self._mode: str = ... - self._set: WebPageSetter = ... + self._set: MixPageSetter = ... self._has_driver: bool = ... self._has_session: bool = ... self._session_options: Union[SessionOptions, None] = ... @@ -171,10 +171,10 @@ class WebPage(SessionPage, ChromiumPage, BasePage): cert: Any | None = ...) -> Union[bool, Response]: ... @property - def latest_tab(self) -> Union[MixTab, WebPage]: ... + def latest_tab(self) -> Union[MixTab, MixPage]: ... @property - def set(self) -> WebPageSetter: ... + def set(self) -> MixPageSetter: ... def _find_elements(self, locator: Union[Tuple[str, str], str, ChromiumElement, SessionElement, ChromiumFrame], diff --git a/DrissionPage/_pages/session_page.py b/DrissionPage/_pages/session_page.py index 0697bea..4e39598 100644 --- a/DrissionPage/_pages/session_page.py +++ b/DrissionPage/_pages/session_page.py @@ -38,6 +38,7 @@ class SessionPage(BasePage): self._encoding = None self._type = 'SessionPage' self._page = self + self._timeout = 10 self._s_set_start_options(session_or_options) self._s_set_runtime_settings() self._create_session() @@ -143,6 +144,11 @@ class SessionPage(BasePage): self._set = SessionPageSetter(self) return self._set + @property + def timeout(self): + """返回超时设置""" + return self._timeout + def get(self, url, show_errmsg=False, retry=None, interval=None, timeout=None, **kwargs): """用get方式跳转到url,可输入文件路径 :param url: 目标url,可指定本地文件路径 diff --git a/DrissionPage/_pages/session_page.pyi b/DrissionPage/_pages/session_page.pyi index 2324ac0..cec9098 100644 --- a/DrissionPage/_pages/session_page.pyi +++ b/DrissionPage/_pages/session_page.pyi @@ -130,6 +130,9 @@ class SessionPage(BasePage): @property def set(self) -> SessionPageSetter: ... + @property + def timeout(self) -> float: ... + def post(self, url: str, show_errmsg: bool = False, diff --git a/DrissionPage/_pages/chromium_tab.py b/DrissionPage/_pages/tabs.py similarity index 98% rename from DrissionPage/_pages/chromium_tab.py rename to DrissionPage/_pages/tabs.py index f41ea7e..80638cf 100644 --- a/DrissionPage/_pages/chromium_tab.py +++ b/DrissionPage/_pages/tabs.py @@ -15,7 +15,7 @@ from .._functions.settings import Settings from .._functions.web import save_page from .._pages.chromium_base import ChromiumBase from .._pages.session_page import SessionPage -from .._units.setter import TabSetter, WebPageTabSetter +from .._units.setter import TabSetter, MixTabSetter from .._units.waiter import TabWaiter @@ -52,7 +52,6 @@ class ChromiumTab(ChromiumBase): def _d_set_runtime_settings(self): """重写设置浏览器运行参数方法""" - print('a') self._timeouts = copy(self.browser.timeouts) self.retry_times = self.browser.retry_times self.retry_interval = self.browser.retry_interval @@ -127,7 +126,7 @@ class MixTab(SessionPage, ChromiumTab, BasePage): def set(self): """返回用于设置的对象""" if self._set is None: - self._set = WebPageTabSetter(self) + self._set = MixTabSetter(self) return self._set @property @@ -208,7 +207,7 @@ class MixTab(SessionPage, ChromiumTab, BasePage): @property def timeout(self): """返回通用timeout设置""" - return self.timeouts.base + return super()._timeout if self._mode == 's' else self.timeouts.base def get(self, url, show_errmsg=False, retry=None, interval=None, timeout=None, **kwargs): """跳转到一个url @@ -374,7 +373,7 @@ class MixTab(SessionPage, ChromiumTab, BasePage): :param locator: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 :param timeout: 查找元素超时时间(秒),d模式专用 :param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有 - :param relative: WebPage用的表示是否相对定位的参数 + :param relative: MixTab用的表示是否相对定位的参数 :param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置 :return: 元素对象或属性、文本节点文本 """ diff --git a/DrissionPage/_pages/chromium_tab.pyi b/DrissionPage/_pages/tabs.pyi similarity index 98% rename from DrissionPage/_pages/chromium_tab.pyi rename to DrissionPage/_pages/tabs.pyi index 7f0a234..3bba12b 100644 --- a/DrissionPage/_pages/chromium_tab.pyi +++ b/DrissionPage/_pages/tabs.pyi @@ -19,7 +19,7 @@ from .._elements.session_element import SessionElement from .._functions.cookies import CookiesList from .._functions.elements import SessionElementsList, ChromiumElementsList from .._units.rect import TabRect -from .._units.setter import TabSetter, WebPageTabSetter +from .._units.setter import TabSetter, MixTabSetter from .._units.waiter import TabWaiter @@ -180,7 +180,7 @@ class MixTab(SessionPage, ChromiumTab): cert: Any | None = ...) -> Union[bool, Response]: ... @property - def set(self) -> WebPageTabSetter: ... + def set(self) -> MixTabSetter: ... def _find_elements(self, locator: Union[Tuple[str, str], str, ChromiumElement, SessionElement, ChromiumFrame], diff --git a/DrissionPage/_units/clicker.pyi b/DrissionPage/_units/clicker.pyi index af3c7b4..557189b 100644 --- a/DrissionPage/_units/clicker.pyi +++ b/DrissionPage/_units/clicker.pyi @@ -10,7 +10,7 @@ from typing import Union from .downloader import DownloadMission from .._elements.chromium_element import ChromiumElement -from .._pages.chromium_tab import MixTab, ChromiumTab +from .._pages.tabs import MixTab, ChromiumTab class Clicker(object): diff --git a/DrissionPage/_units/cookies_setter.py b/DrissionPage/_units/cookies_setter.py index cb48d53..cc100c2 100644 --- a/DrissionPage/_units/cookies_setter.py +++ b/DrissionPage/_units/cookies_setter.py @@ -79,7 +79,7 @@ class SessionCookiesSetter(object): self._owner.session.cookies.clear() -class WebPageCookiesSetter(CookiesSetter, SessionCookiesSetter): +class MixPageCookiesSetter(CookiesSetter, SessionCookiesSetter): def __call__(self, cookies): """设置多个cookie,注意不要传入单个 diff --git a/DrissionPage/_units/cookies_setter.pyi b/DrissionPage/_units/cookies_setter.pyi index dcc4215..3eb6c40 100644 --- a/DrissionPage/_units/cookies_setter.pyi +++ b/DrissionPage/_units/cookies_setter.pyi @@ -10,9 +10,9 @@ from typing import Union from .._base.browser import Chromium from .._pages.chromium_base import ChromiumBase -from .._pages.chromium_tab import MixTab +from .._pages.tabs import MixTab from .._pages.session_page import SessionPage -from .._pages.web_page import WebPage +from .._pages.mix_page import MixPage class CookiesSetter(object): @@ -45,8 +45,8 @@ class SessionCookiesSetter(object): def clear(self) -> None: ... -class WebPageCookiesSetter(CookiesSetter, SessionCookiesSetter): - _owner: Union[WebPage, MixTab] = ... +class MixPageCookiesSetter(CookiesSetter, SessionCookiesSetter): + _owner: Union[MixPage, MixTab] = ... def __init__(self, page: SessionPage): ... diff --git a/DrissionPage/_units/rect.pyi b/DrissionPage/_units/rect.pyi index 0b2c767..abd6047 100644 --- a/DrissionPage/_units/rect.pyi +++ b/DrissionPage/_units/rect.pyi @@ -12,8 +12,8 @@ from .._elements.chromium_element import ChromiumElement from .._pages.chromium_base import ChromiumBase from .._pages.chromium_frame import ChromiumFrame from .._pages.chromium_page import ChromiumPage -from .._pages.chromium_tab import ChromiumTab, MixTab -from .._pages.web_page import WebPage +from .._pages.tabs import ChromiumTab, MixTab +from .._pages.mix_page import MixPage class ElementRect(object): @@ -66,7 +66,7 @@ class ElementRect(object): class TabRect(object): def __init__(self, owner: ChromiumBase): - self._owner: Union[ChromiumPage, ChromiumTab, WebPage, MixTab] = ... + self._owner: Union[ChromiumPage, ChromiumTab, MixPage, MixTab] = ... @property def window_state(self) -> str: ... diff --git a/DrissionPage/_units/setter.py b/DrissionPage/_units/setter.py index 25a7571..3f0e441 100644 --- a/DrissionPage/_units/setter.py +++ b/DrissionPage/_units/setter.py @@ -10,14 +10,14 @@ from time import sleep from requests.structures import CaseInsensitiveDict -from .cookies_setter import SessionCookiesSetter, CookiesSetter, WebPageCookiesSetter, BrowserCookiesSetter +from .cookies_setter import SessionCookiesSetter, CookiesSetter, MixPageCookiesSetter, BrowserCookiesSetter from .._functions.settings import Settings from .._functions.tools import show_or_hide_browser from .._functions.web import format_headers from ..errors import ElementLostError, JavaScriptError -class BasePageSetter(object): +class BaseSetter(object): def __init__(self, owner): """ :param owner: BasePage对象 @@ -51,7 +51,144 @@ class BasePageSetter(object): self._owner._download_path = str(Path(path).absolute()) -class BrowserBaseSetter(BasePageSetter): +class SessionPageSetter(BaseSetter): + def __init__(self, owner): + """ + :param owner: SessionPage对象 + """ + super().__init__(owner) + self._cookies_setter = None + + @property + def cookies(self): + """返回用于设置cookies的对象""" + if self._cookies_setter is None: + self._cookies_setter = SessionCookiesSetter(self._owner) + return self._cookies_setter + + def download_path(self, path): + """设置下载路径 + :param path: 下载路径 + :return: None + """ + super().download_path(path) + if self._owner._DownloadKit: + self._owner._DownloadKit.set.goal_path(self._owner._download_path) + + def timeout(self, second): + """设置连接超时时间 + :param second: 秒数 + :return: None + """ + self._owner._timeout = second + + def encoding(self, encoding, set_all=True): + """设置编码 + :param encoding: 编码名称,如果要取消之前的设置,传入None + :param set_all: 是否设置对象参数,为False则只设置当前Response + :return: None + """ + if set_all: + self._owner._encoding = encoding if encoding else None + if self._owner.response: + self._owner.response.encoding = encoding + + def headers(self, headers): + """设置通用的headers + :param headers: dict形式的headers + :return: None + """ + self._owner._headers = CaseInsensitiveDict(format_headers(headers)) + + def header(self, name, value): + """设置headers中一个项 + :param name: 设置名称 + :param value: 设置值 + :return: None + """ + self._owner._headers[name] = value + + def user_agent(self, ua): + """设置user agent + :param ua: user agent + :return: None + """ + self._owner._headers['user-agent'] = ua + + def proxies(self, http=None, https=None): + """设置proxies参数 + :param http: http代理地址 + :param https: https代理地址 + :return: None + """ + self._owner.session.proxies = {'http': http, 'https': https} + + def auth(self, auth): + """设置认证元组或对象 + :param auth: 认证元组或对象 + :return: None + """ + self._owner.session.auth = auth + + def hooks(self, hooks): + """设置回调方法 + :param hooks: 回调方法 + :return: None + """ + self._owner.session.hooks = hooks + + def params(self, params): + """设置查询参数字典 + :param params: 查询参数字典 + :return: None + """ + self._owner.session.params = params + + def verify(self, on_off): + """设置是否验证SSL证书 + :param on_off: 是否验证 SSL 证书 + :return: None + """ + self._owner.session.verify = on_off + + def cert(self, cert): + """SSL客户端证书文件的路径(.pem格式),或(‘cert’, ‘key’)元组 + :param cert: 证书路径或元组 + :return: None + """ + self._owner.session.cert = cert + + def stream(self, on_off): + """设置是否使用流式响应内容 + :param on_off: 是否使用流式响应内容 + :return: None + """ + self._owner.session.stream = on_off + + def trust_env(self, on_off): + """设置是否信任环境 + :param on_off: 是否信任环境 + :return: None + """ + self._owner.session.trust_env = on_off + + def max_redirects(self, times): + """设置最大重定向次数 + :param times: 最大重定向次数 + :return: None + """ + self._owner.session.max_redirects = times + + def add_adapter(self, url, adapter): + """添加适配器 + :param url: 适配器对应url + :param adapter: 适配器对象 + :return: None + """ + self._owner.session.mount(url, adapter) + + +class BrowserBaseSetter(BaseSetter): """Browser和ChromiumBase设置""" def __init__(self, owner): @@ -314,144 +451,7 @@ class ChromiumPageSetter(TabSetter): self._owner._alert.auto = accept if on_off else None -class SessionPageSetter(BasePageSetter): - def __init__(self, owner): - """ - :param owner: SessionPage对象 - """ - super().__init__(owner) - self._cookies_setter = None - - @property - def cookies(self): - """返回用于设置cookies的对象""" - if self._cookies_setter is None: - self._cookies_setter = SessionCookiesSetter(self._owner) - return self._cookies_setter - - def download_path(self, path): - """设置下载路径 - :param path: 下载路径 - :return: None - """ - super().download_path(path) - if self._owner._DownloadKit: - self._owner._DownloadKit.set.goal_path(self._owner._download_path) - - def timeout(self, second): - """设置连接超时时间 - :param second: 秒数 - :return: None - """ - self._owner._timeout = second - - def encoding(self, encoding, set_all=True): - """设置编码 - :param encoding: 编码名称,如果要取消之前的设置,传入None - :param set_all: 是否设置对象参数,为False则只设置当前Response - :return: None - """ - if set_all: - self._owner._encoding = encoding if encoding else None - if self._owner.response: - self._owner.response.encoding = encoding - - def headers(self, headers): - """设置通用的headers - :param headers: dict形式的headers - :return: None - """ - self._owner._headers = CaseInsensitiveDict(format_headers(headers)) - - def header(self, name, value): - """设置headers中一个项 - :param name: 设置名称 - :param value: 设置值 - :return: None - """ - self._owner._headers[name] = value - - def user_agent(self, ua): - """设置user agent - :param ua: user agent - :return: None - """ - self._owner._headers['user-agent'] = ua - - def proxies(self, http=None, https=None): - """设置proxies参数 - :param http: http代理地址 - :param https: https代理地址 - :return: None - """ - self._owner.session.proxies = {'http': http, 'https': https} - - def auth(self, auth): - """设置认证元组或对象 - :param auth: 认证元组或对象 - :return: None - """ - self._owner.session.auth = auth - - def hooks(self, hooks): - """设置回调方法 - :param hooks: 回调方法 - :return: None - """ - self._owner.session.hooks = hooks - - def params(self, params): - """设置查询参数字典 - :param params: 查询参数字典 - :return: None - """ - self._owner.session.params = params - - def verify(self, on_off): - """设置是否验证SSL证书 - :param on_off: 是否验证 SSL 证书 - :return: None - """ - self._owner.session.verify = on_off - - def cert(self, cert): - """SSL客户端证书文件的路径(.pem格式),或(‘cert’, ‘key’)元组 - :param cert: 证书路径或元组 - :return: None - """ - self._owner.session.cert = cert - - def stream(self, on_off): - """设置是否使用流式响应内容 - :param on_off: 是否使用流式响应内容 - :return: None - """ - self._owner.session.stream = on_off - - def trust_env(self, on_off): - """设置是否信任环境 - :param on_off: 是否信任环境 - :return: None - """ - self._owner.session.trust_env = on_off - - def max_redirects(self, times): - """设置最大重定向次数 - :param times: 最大重定向次数 - :return: None - """ - self._owner.session.max_redirects = times - - def add_adapter(self, url, adapter): - """添加适配器 - :param url: 适配器对应url - :param adapter: 适配器对象 - :return: None - """ - self._owner.session.mount(url, adapter) - - -class WebPageSetter(ChromiumPageSetter): +class MixPageSetter(ChromiumPageSetter): def __init__(self, owner): super().__init__(owner) self._session_setter = SessionPageSetter(self._owner) @@ -461,7 +461,7 @@ class WebPageSetter(ChromiumPageSetter): def cookies(self): """返回用于设置cookies的对象""" if self._cookies_setter is None: - self._cookies_setter = WebPageCookiesSetter(self._owner) + self._cookies_setter = MixPageCookiesSetter(self._owner) return self._cookies_setter def headers(self, headers) -> None: @@ -482,7 +482,7 @@ class WebPageSetter(ChromiumPageSetter): self._chromium_setter.user_agent(ua, platform) -class WebPageTabSetter(TabSetter): +class MixTabSetter(TabSetter): def __init__(self, owner): super().__init__(owner) self._session_setter = SessionPageSetter(self._owner) @@ -492,7 +492,7 @@ class WebPageTabSetter(TabSetter): def cookies(self): """返回用于设置cookies的对象""" if self._cookies_setter is None: - self._cookies_setter = WebPageCookiesSetter(self._owner) + self._cookies_setter = MixPageCookiesSetter(self._owner) return self._cookies_setter def headers(self, headers) -> None: @@ -512,6 +512,17 @@ class WebPageTabSetter(TabSetter): if self._owner._has_driver: self._chromium_setter.user_agent(ua, platform) + def timeouts(self, base=None, page_load=None, script=None): + """设置超时时间,单位为秒 + :param base: 基本等待时间,除页面加载和脚本超时,其它等待默认使用 + :param page_load: 页面加载超时时间 + :param script: 脚本运行超时时间 + :return: None + """ + super().timeouts(base=base, page_load=page_load, script=script) + if base is not None: + self._owner._timeout = base + class ChromiumElementSetter(object): def __init__(self, ele): diff --git a/DrissionPage/_units/setter.pyi b/DrissionPage/_units/setter.pyi index 1653ae7..c6304b5 100644 --- a/DrissionPage/_units/setter.pyi +++ b/DrissionPage/_units/setter.pyi @@ -11,7 +11,7 @@ from typing import Union, Tuple, Literal, Any, Optional from requests.adapters import HTTPAdapter from requests.auth import HTTPBasicAuth -from .cookies_setter import SessionCookiesSetter, CookiesSetter, WebPageCookiesSetter, BrowserCookiesSetter +from .cookies_setter import SessionCookiesSetter, CookiesSetter, MixPageCookiesSetter, BrowserCookiesSetter from .scroller import PageScroller from .._base.base import BasePage from .._base.browser import Chromium @@ -19,14 +19,14 @@ from .._elements.chromium_element import ChromiumElement from .._pages.chromium_base import ChromiumBase from .._pages.chromium_frame import ChromiumFrame from .._pages.chromium_page import ChromiumPage -from .._pages.chromium_tab import ChromiumTab, MixTab +from .._pages.tabs import ChromiumTab, MixTab from .._pages.session_page import SessionPage -from .._pages.web_page import WebPage +from .._pages.mix_page import MixPage FILE_EXISTS = Literal['skip', 'rename', 'overwrite', 's', 'r', 'o'] -class BasePageSetter(object): +class BaseSetter(object): def __init__(self, owner: Union[Chromium, BasePage]): self._owner: Union[Chromium, BasePage] = ... @@ -39,95 +39,7 @@ class BasePageSetter(object): def download_path(self, path: Union[str, Path, None]) -> None: ... -class BrowserBaseSetter(BasePageSetter): - _cookies_setter: Optional[CookiesSetter] = ... - - @property - def load_mode(self) -> LoadMode: ... - - def timeouts(self, base=None, page_load=None, script=None) -> None: ... - - -class BrowserSetter(BrowserBaseSetter): - _owner: Chromium = ... - _cookies_setter: BrowserCookiesSetter = ... - - def tab_to_front(self, tab_or_id: Union[str, ChromiumTab]) -> None: ... - - @property - def cookies(self) -> BrowserCookiesSetter: ... - - def auto_handle_alert(self, on_off: bool = True, accept: bool = True): ... - - def download_path(self, path: Union[Path, str, None]): ... - - def download_file_name(self, name: str = None, suffix: str = None): ... - - def when_download_file_exists(self, mode: FILE_EXISTS): ... - - -class ChromiumBaseSetter(BasePageSetter): - _owner: ChromiumBase = ... - _cookies_setter: CookiesSetter = ... - - def __init__(self, owner): ... - - @property - def load_mode(self) -> LoadMode: ... - - @property - def scroll(self) -> PageScrollSetter: ... - - @property - def cookies(self) -> CookiesSetter: ... - - def retry_times(self, times: int) -> None: ... - - def retry_interval(self, interval: float) -> None: ... - - def timeouts(self, base: float = None, page_load: float = None, script: float = None) -> None: ... - - def user_agent(self, ua: str, platform: str = None) -> None: ... - - def session_storage(self, item: str, value: Union[str, bool]) -> None: ... - - def local_storage(self, item: str, value: Union[str, bool]) -> None: ... - - def headers(self, headers: Union[dict, str]) -> None: ... - - def auto_handle_alert(self, on_off: bool = True, accept: bool = True) -> None: ... - - def upload_files(self, files: Union[str, Path, list, tuple]) -> None: ... - - def blocked_urls(self, urls: Union[list, tuple, str, None]) -> None: ... - - -class TabSetter(ChromiumBaseSetter): - _owner: ChromiumTab = ... - - def __init__(self, owner: Union[ChromiumTab, MixTab, WebPage, ChromiumPage]): ... - - @property - def window(self) -> WindowSetter: ... - - def download_path(self, path: Union[str, Path, None]) -> None: ... - - def download_file_name(self, name: str = None, suffix: str = None) -> None: ... - - def when_download_file_exists(self, mode: FILE_EXISTS) -> None: ... - - def activate(self) -> None: ... - - -class ChromiumPageSetter(TabSetter): - _owner: ChromiumPage = ... - - def tab_to_front(self, tab_or_id: Union[str, ChromiumTab] = None) -> None: ... - - def auto_handle_alert(self, on_off: bool = True, accept: bool = True, all_tabs: bool = False) -> None: ... - - -class SessionPageSetter(BasePageSetter): +class SessionPageSetter(BaseSetter): _owner: SessionPage = ... _cookies_setter: Optional[SessionCookiesSetter] = ... @@ -173,8 +85,87 @@ class SessionPageSetter(BasePageSetter): def add_adapter(self, url: str, adapter: HTTPAdapter) -> None: ... -class WebPageSetter(ChromiumPageSetter): - _owner: WebPage = ... +class BrowserBaseSetter(BaseSetter): + _cookies_setter: Optional[CookiesSetter] = ... + + @property + def load_mode(self) -> LoadMode: ... + + def timeouts(self, base=None, page_load=None, script=None) -> None: ... + + +class BrowserSetter(BrowserBaseSetter): + _owner: Chromium = ... + _cookies_setter: BrowserCookiesSetter = ... + + def tab_to_front(self, tab_or_id: Union[str, ChromiumTab]) -> None: ... + + @property + def cookies(self) -> BrowserCookiesSetter: ... + + def auto_handle_alert(self, on_off: bool = True, accept: bool = True): ... + + def download_path(self, path: Union[Path, str, None]): ... + + def download_file_name(self, name: str = None, suffix: str = None): ... + + def when_download_file_exists(self, mode: FILE_EXISTS): ... + + +class ChromiumBaseSetter(BrowserBaseSetter): + _owner: ChromiumBase = ... + _cookies_setter: CookiesSetter = ... + + def __init__(self, owner): ... + + @property + def scroll(self) -> PageScrollSetter: ... + + @property + def cookies(self) -> CookiesSetter: ... + + def user_agent(self, ua: str, platform: str = None) -> None: ... + + def session_storage(self, item: str, value: Union[str, bool]) -> None: ... + + def local_storage(self, item: str, value: Union[str, bool]) -> None: ... + + def headers(self, headers: Union[dict, str]) -> None: ... + + def auto_handle_alert(self, on_off: bool = True, accept: bool = True) -> None: ... + + def upload_files(self, files: Union[str, Path, list, tuple]) -> None: ... + + def blocked_urls(self, urls: Union[list, tuple, str, None]) -> None: ... + + +class TabSetter(ChromiumBaseSetter): + _owner: ChromiumTab = ... + + def __init__(self, owner: Union[ChromiumTab, MixTab, MixPage, ChromiumPage]): ... + + @property + def window(self) -> WindowSetter: ... + + def download_path(self, path: Union[str, Path, None]) -> None: ... + + def download_file_name(self, name: str = None, suffix: str = None) -> None: ... + + def when_download_file_exists(self, mode: FILE_EXISTS) -> None: ... + + def activate(self) -> None: ... + + +class ChromiumPageSetter(TabSetter): + _owner: ChromiumPage = ... + + def tab_to_front(self, tab_or_id: Union[str, ChromiumTab] = None) -> None: ... + + def auto_handle_alert(self, on_off: bool = True, accept: bool = True, all_tabs: bool = False) -> None: ... + + +class MixPageSetter(ChromiumPageSetter): + _owner: MixPage = ... _session_setter: SessionPageSetter = ... _chromium_setter: ChromiumPageSetter = ... @@ -183,10 +174,10 @@ class WebPageSetter(ChromiumPageSetter): def headers(self, headers: Union[str, dict]) -> None: ... @property - def cookies(self) -> WebPageCookiesSetter: ... + def cookies(self) -> MixPageCookiesSetter: ... -class WebPageTabSetter(TabSetter): +class MixTabSetter(TabSetter): _owner: MixTab = ... _session_setter: SessionPageSetter = ... _chromium_setter: ChromiumBaseSetter = ... @@ -196,7 +187,9 @@ class WebPageTabSetter(TabSetter): def headers(self, headers: Union[str, dict]) -> None: ... @property - def cookies(self) -> WebPageCookiesSetter: ... + def cookies(self) -> MixPageCookiesSetter: ... + + def timeouts(self, base: float = None, page_load: float = None, script: float = None) -> None: ... class ChromiumElementSetter(object): diff --git a/DrissionPage/items.py b/DrissionPage/items.py index 5b680e8..6881812 100644 --- a/DrissionPage/items.py +++ b/DrissionPage/items.py @@ -9,7 +9,7 @@ from ._elements.chromium_element import ChromiumElement, ShadowRoot from ._elements.none_element import NoneElement from ._elements.session_element import SessionElement from ._pages.chromium_frame import ChromiumFrame -from ._pages.chromium_tab import ChromiumTab, MixTab +from ._pages.tabs import ChromiumTab, MixTab __all__ = ['ChromiumElement', 'ShadowRoot', 'NoneElement', 'SessionElement', 'ChromiumFrame', 'ChromiumTab', 'MixTab']