From 65d561e0791733e87ca9c26219e97bd7b8d43dba Mon Sep 17 00:00:00 2001 From: g1879 Date: Mon, 14 Oct 2024 00:01:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DPage=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E4=B8=8B=E8=BD=BD=E8=B7=AF=E5=BE=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=9B=20=E4=BC=98=E5=8C=96=E7=AD=89=E5=BE=85?= =?UTF-8?q?=E6=96=B0tab=E9=80=BB=E8=BE=91=EF=BC=9B=20=E6=8E=A5=E7=AE=A1?= =?UTF-8?q?=E6=9D=A5=E8=87=AAselenium=E5=92=8Cplaywright=E7=9A=84=E6=B5=8F?= =?UTF-8?q?=E8=A7=88=E5=99=A8=E6=97=B6=E5=BF=BD=E7=95=A5=E6=97=A0=E5=A4=B4?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=EF=BC=9B=20=E4=BC=98=E5=8C=96=E5=85=B3?= =?UTF-8?q?=E9=97=ADtab=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_base/chromium.py | 16 +++++++++++----- DrissionPage/_base/chromium.pyi | 8 ++++++++ DrissionPage/_pages/chromium_page.py | 2 +- DrissionPage/_pages/chromium_tab.py | 5 ++++- DrissionPage/_pages/web_page.py | 2 +- DrissionPage/_units/clicker.py | 2 +- DrissionPage/_units/setter.py | 2 +- DrissionPage/_units/waiter.py | 7 +++---- DrissionPage/common.py | 9 +++++++-- 9 files changed, 37 insertions(+), 16 deletions(-) diff --git a/DrissionPage/_base/chromium.py b/DrissionPage/_base/chromium.py index 35217cd..082f764 100644 --- a/DrissionPage/_base/chromium.py +++ b/DrissionPage/_base/chromium.py @@ -64,6 +64,8 @@ class Chromium(object): self._frames = {} self._drivers = {} self._all_drivers = {} + self._newest_tab_id = None + self._tab_to_close = set() self._set = None self._wait = None @@ -211,12 +213,14 @@ class Chromium(object): return for tab in tabs: - self._onTargetDestroyed(targetId=tab) - self._driver.run('Target.closeTarget', targetId=tab) + self._close_tab(tab_id=tab) + + def _close_tab(self, tab_id): + # self._onTargetDestroyed(targetId=tab) + self._tab_to_close.add(tab_id) + self._driver.run('Target.closeTarget', targetId=tab_id) + while tab_id in self._tab_to_close: sleep(.2) - end_time = perf_counter() + 3 - while self.tabs_count != end_len and perf_counter() < end_time: - sleep(.1) def activate_tab(self, id_ind_tab): if isinstance(id_ind_tab, int): @@ -392,6 +396,7 @@ class Chromium(object): d = Driver(tab_id, 'page', self.address) self._drivers[tab_id] = d self._all_drivers.setdefault(tab_id, set()).add(d) + self._newest_tab_id = tab_id except WebSocketBadStatusException: pass @@ -404,6 +409,7 @@ class Chromium(object): d.stop() self._drivers.pop(tab_id, None) self._all_drivers.pop(tab_id, None) + self._tab_to_close.discard(tab_id) def _on_disconnect(self): if not self._disconnect_flag: diff --git a/DrissionPage/_base/chromium.pyi b/DrissionPage/_base/chromium.pyi index 9c613d5..b3b9ee8 100644 --- a/DrissionPage/_base/chromium.pyi +++ b/DrissionPage/_base/chromium.pyi @@ -51,6 +51,8 @@ class Chromium(object): _disconnect_flag: bool = ... _none_ele_return_value: bool = ... _none_ele_value: Any = ... + _newest_tab_id: Optional[str] = ... + _tab_to_close: set = ... def __new__(cls, addr_or_opts: Union[str, int, ChromiumOptions] = None, @@ -192,6 +194,12 @@ class Chromium(object): """ ... + def _close_tab(self, tab_id: str): + """关闭一个标签页 + :param tab_id: 标签页id + :return: None + """ + def activate_tab(self, id_ind_tab: Union[int, str, ChromiumTab]) -> None: """使一个标签页显示到前端 :param id_ind_tab: 标签页id(str)、Tab对象或标签页序号(int),序号从1开始 diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index a6c66b2..360c98a 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -114,7 +114,7 @@ class ChromiumPage(ChromiumBase): self.browser.activate_tab(id_ind_tab) def close(self): - self.close_tabs(self.tab_id) + self.browser._close_tab(self.tab_id) def close_tabs(self, tabs_or_ids=None, others=False): self.browser.close_tabs(tabs_or_ids=tabs_or_ids or self.tab_id, others=others) diff --git a/DrissionPage/_pages/chromium_tab.py b/DrissionPage/_pages/chromium_tab.py index 0d6e72e..878ca4d 100644 --- a/DrissionPage/_pages/chromium_tab.py +++ b/DrissionPage/_pages/chromium_tab.py @@ -51,7 +51,10 @@ class ChromiumTab(ChromiumBase): self._none_ele_value = self.browser._none_ele_value def close(self, others=False): - self.browser.close_tabs(self.tab_id, others=others) + if others: + self.browser.close_tabs(self.tab_id, others=True) + else: + self.browser._close_tab(self.tab_id) @property def set(self): diff --git a/DrissionPage/_pages/web_page.py b/DrissionPage/_pages/web_page.py index c6f9411..bfe1966 100644 --- a/DrissionPage/_pages/web_page.py +++ b/DrissionPage/_pages/web_page.py @@ -228,7 +228,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def close(self): if self._has_driver: - self.close_tabs(self.tab_id) + self.browser._close_tab(self.tab_id) if self._session: self._session.close() if self._response is not None: diff --git a/DrissionPage/_units/clicker.py b/DrissionPage/_units/clicker.py index 38cbd0b..9097080 100644 --- a/DrissionPage/_units/clicker.py +++ b/DrissionPage/_units/clicker.py @@ -144,7 +144,7 @@ class Clicker(object): self._ele.owner.wait.upload_paths_inputted() def for_new_tab(self, by_js=False, timeout=3): - curr_tid = self._ele.tab.browser.tab_ids[0] + curr_tid = self._ele.tab.browser._newest_tab_id self.left(by_js=by_js) tid = self._ele.tab.browser.wait.new_tab(timeout=timeout, curr_tab=curr_tid) if not tid: diff --git a/DrissionPage/_units/setter.py b/DrissionPage/_units/setter.py index 0eccf16..acff5ac 100644 --- a/DrissionPage/_units/setter.py +++ b/DrissionPage/_units/setter.py @@ -264,7 +264,7 @@ class ChromiumPageSetter(TabSetter): def download_path(self, path): super().download_path(path) - self._owner.browser._download_path = self._owner._download_path + self._owner.browser.set.download_path(path) class WebPageSetter(ChromiumPageSetter): diff --git a/DrissionPage/_units/waiter.py b/DrissionPage/_units/waiter.py index 6691484..60b41d8 100644 --- a/DrissionPage/_units/waiter.py +++ b/DrissionPage/_units/waiter.py @@ -28,16 +28,15 @@ class OriginWaiter(object): class BrowserWaiter(OriginWaiter): def new_tab(self, timeout=None, curr_tab=None, raise_err=None): if not curr_tab: - curr_tab = self._owner.tab_ids[0] + curr_tab = self._owner._newest_tab_id elif hasattr(curr_tab, '_type'): curr_tab = curr_tab.tab_id if timeout is None: timeout = self._owner.timeout end_time = perf_counter() + timeout while perf_counter() < end_time: - latest_tid = self._owner.tab_ids[0] - if curr_tab != latest_tid: - return latest_tid + if curr_tab != self._owner._newest_tab_id: + return self._owner._newest_tab_id sleep(.01) if raise_err is True or Settings.raise_when_wait_failed is True: diff --git a/DrissionPage/common.py b/DrissionPage/common.py index a412f8c..15e11ef 100644 --- a/DrissionPage/common.py +++ b/DrissionPage/common.py @@ -6,6 +6,7 @@ @License : BSD 3-Clause. """ from ._base.chromium import Chromium +from ._configs.chromium_options import ChromiumOptions from ._elements.session_element import make_session_ele from ._functions.by import By from ._functions.keys import Keys @@ -23,7 +24,9 @@ def from_selenium(driver): address, port = driver.caps.get('goog:chromeOptions', {}).get('debuggerAddress', ':').split(':') if not address: raise RuntimeError('获取失败。') - return Chromium(f'{address}:{port}') + co = ChromiumOptions().set_local_port(f'{address}:{port}') + co._ua_set = True + return Chromium(co) def from_playwright(page_or_browser): @@ -48,4 +51,6 @@ def from_playwright(page_or_browser): break else: raise RuntimeError('获取失败,请用管理员权限运行。') - return Chromium(f'127.0.0.1:{port}') + co = ChromiumOptions().set_local_port(f'127.0.0.1:{port}') + co._ua_set = True + return Chromium(co)