From 2092f7a6a3e6b497beaa3c13633db4b27a5a21fe Mon Sep 17 00:00:00 2001 From: g1879 Date: Thu, 9 Dec 2021 17:29:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/driver_element.py | 6 +++++- DrissionPage/driver_page.py | 11 ++++++++--- DrissionPage/mix_page.py | 4 ++-- DrissionPage/session_element.py | 13 +++++++++---- DrissionPage/session_page.py | 7 ++++--- README.md | 1 + 6 files changed, 29 insertions(+), 13 deletions(-) diff --git a/DrissionPage/driver_element.py b/DrissionPage/driver_element.py index b3f7842..9429137 100644 --- a/DrissionPage/driver_element.py +++ b/DrissionPage/driver_element.py @@ -24,10 +24,14 @@ class DriverElement(DrissionElement): """driver模式的元素对象,包装了一个WebElement对象,并封装了常用功能""" def __init__(self, ele: WebElement, page=None): + """初始化对象 \n + :param ele: 被包装的WebElement元素 + :param page: 元素所在页面 + """ super().__init__(ele, page) self._select = None - def __repr__(self): + def __repr__(self) -> str: attrs = [f"{attr}='{self.attrs[attr]}'" for attr in self.attrs] return f'' diff --git a/DrissionPage/driver_page.py b/DrissionPage/driver_page.py index e441c7d..58a863a 100644 --- a/DrissionPage/driver_page.py +++ b/DrissionPage/driver_page.py @@ -274,9 +274,14 @@ class DriverPage(BasePage): :param url: 新标签页跳转到的网址 :return: None """ - self.to_tab(-1) - self.run_script(f'window.open("{url}");') - self.to_tab(-1) + try: # selenium4新增功能,须配合新版浏览器 + self.driver.switch_to.new_window('tab') + if url: + self.get(url) + except: + self.to_tab(-1) + self.run_script(f'window.open("{url}");') + self.to_tab(-1) def close_current_tab(self) -> None: """关闭当前标签页""" diff --git a/DrissionPage/mix_page.py b/DrissionPage/mix_page.py index e1f443a..6af3eec 100644 --- a/DrissionPage/mix_page.py +++ b/DrissionPage/mix_page.py @@ -408,10 +408,10 @@ class MixPage(SessionPage, DriverPage, BasePage): return super().chrome_downloading(path) # ----------------MixPage独有函数----------------------- - def hide_browser(self): + def hide_browser(self) -> None: """隐藏浏览器窗口""" self.drission.hide_browser() - def show_browser(self): + def show_browser(self) -> None: """显示浏览器窗口""" self.drission.show_browser() diff --git a/DrissionPage/session_element.py b/DrissionPage/session_element.py index 16b295b..0c9c831 100644 --- a/DrissionPage/session_element.py +++ b/DrissionPage/session_element.py @@ -19,16 +19,21 @@ class SessionElement(DrissionElement): """session模式的元素对象,包装了一个lxml的Element对象,并封装了常用功能""" def __init__(self, ele: HtmlElement, page=None): + """初始化对象 \n + :param ele: 被包装的HtmlElement元素 + :param page: 元素所在页面对象,如果是从 html 文本生成的元素,则为 None + """ super().__init__(ele, page) - def __repr__(self): + def __repr__(self) -> str: attrs = [f"{attr}='{self.attrs[attr]}'" for attr in self.attrs] return f'' - def __call__(self, loc_or_str: Union[Tuple[str, str], str]): - """在内部查找元素 \n - 例:ele2 = ele1('@id=ele_id') \n + def __call__(self, loc_or_str: Union[Tuple[str, str], str], timeout=None): + """在内部查找元素 \n + 例:ele2 = ele1('@id=ele_id') \n :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param timeout: 不起实际作用,用于和DriverElement对应,便于无差别调用 :return: SessionElement对象或属性、文本 """ return self.ele(loc_or_str) diff --git a/DrissionPage/session_page.py b/DrissionPage/session_page.py index 20a8a27..09ad26a 100644 --- a/DrissionPage/session_page.py +++ b/DrissionPage/session_page.py @@ -30,11 +30,12 @@ class SessionPage(BasePage): self._session = session self._response = None - def __call__(self, loc_or_str: Union[Tuple[str, str], str, SessionElement]) \ + def __call__(self, loc_or_str: Union[Tuple[str, str], str, SessionElement], timeout=None) \ -> Union[SessionElement, List[SessionElement], str]: - """在内部查找元素 \n - 例:ele2 = ele1('@id=ele_id') \n + """在内部查找元素 \n + 例:ele2 = ele1('@id=ele_id') \n :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param timeout: 不起实际作用,用于和DriverElement对应,便于无差别调用 :return: SessionElement对象或属性文本 """ return self.ele(loc_or_str) diff --git a/README.md b/README.md index de1a46e..a9cc7cb 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ requests 爬虫面对要登录的网站时,要分析数据包、JS 源码, - 输出的数据均已转码及处理基本排版,减少重复劳动。 - 可方便地与 selenium 或 requests 原生代码对接,便于项目迁移。 - 使用 POM 模式封装,可直接用于测试,便于扩展。 +- d 模式配置可同时兼容 debugger 和其它参数,原生不能兼容。 - 还有很多这里不一一列举………… # 简单演示