mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
4.0.3.1增加reconnect()方法;优化关闭标签页逻辑;修复s_ele()后定位问题
This commit is contained in:
parent
8ec59c3293
commit
f485cfc7a6
@ -14,4 +14,4 @@ from ._configs.chromium_options import ChromiumOptions
|
|||||||
from ._configs.session_options import SessionOptions
|
from ._configs.session_options import SessionOptions
|
||||||
|
|
||||||
__all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__']
|
__all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__']
|
||||||
__version__ = '4.0.3'
|
__version__ = '4.0.3.1'
|
||||||
|
@ -49,7 +49,7 @@ class Browser(object):
|
|||||||
self.id = browser_id
|
self.id = browser_id
|
||||||
self._frames = {}
|
self._frames = {}
|
||||||
self._drivers = {}
|
self._drivers = {}
|
||||||
# self._drivers = {t: Driver(t, 'page', address) for t in self.tabs}
|
self._all_drivers = {}
|
||||||
self._connected = False
|
self._connected = False
|
||||||
|
|
||||||
self._process_id = None
|
self._process_id = None
|
||||||
@ -64,20 +64,24 @@ class Browser(object):
|
|||||||
self._driver.set_callback('Target.targetCreated', self._onTargetCreated)
|
self._driver.set_callback('Target.targetCreated', self._onTargetCreated)
|
||||||
|
|
||||||
def _get_driver(self, tab_id, owner=None):
|
def _get_driver(self, tab_id, owner=None):
|
||||||
"""获取对应tab id的Driver
|
"""新建并返回指定tab id的Driver
|
||||||
:param tab_id: 标签页id
|
:param tab_id: 标签页id
|
||||||
:param owner: 使用该驱动的对象
|
:param owner: 使用该驱动的对象
|
||||||
:return: Driver对象
|
:return: Driver对象
|
||||||
"""
|
"""
|
||||||
return self._drivers.pop(tab_id, Driver(tab_id, 'page', self.address, owner))
|
d = self._drivers.pop(tab_id, Driver(tab_id, 'page', self.address, owner))
|
||||||
|
self._all_drivers.setdefault(tab_id, set()).add(d)
|
||||||
|
return d
|
||||||
|
|
||||||
def _onTargetCreated(self, **kwargs):
|
def _onTargetCreated(self, **kwargs):
|
||||||
"""标签页创建时执行"""
|
"""标签页创建时执行"""
|
||||||
if (kwargs['targetInfo']['type'] in ('page', 'webview')
|
if (kwargs['targetInfo']['type'] in ('page', 'webview')
|
||||||
and not kwargs['targetInfo']['url'].startswith('devtools://')):
|
and not kwargs['targetInfo']['url'].startswith('devtools://')):
|
||||||
try:
|
try:
|
||||||
self._drivers[kwargs['targetInfo']['targetId']] = Driver(kwargs['targetInfo']['targetId'],
|
tab_id = kwargs['targetInfo']['targetId']
|
||||||
'page', self.address)
|
d = Driver(tab_id, 'page', self.address)
|
||||||
|
self._drivers[tab_id] = d
|
||||||
|
self._all_drivers.setdefault(tab_id, set()).add(d)
|
||||||
except WebSocketBadStatusException:
|
except WebSocketBadStatusException:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -88,7 +92,10 @@ class Browser(object):
|
|||||||
self._dl_mgr.clear_tab_info(tab_id)
|
self._dl_mgr.clear_tab_info(tab_id)
|
||||||
for key in [k for k, i in self._frames.items() if i == tab_id]:
|
for key in [k for k, i in self._frames.items() if i == tab_id]:
|
||||||
self._frames.pop(key, None)
|
self._frames.pop(key, None)
|
||||||
|
for d in self._all_drivers.get(tab_id, tuple()):
|
||||||
|
d.stop()
|
||||||
self._drivers.pop(tab_id, None)
|
self._drivers.pop(tab_id, None)
|
||||||
|
self._all_drivers.pop(tab_id, None)
|
||||||
|
|
||||||
def connect_to_page(self):
|
def connect_to_page(self):
|
||||||
"""执行与page相关的逻辑"""
|
"""执行与page相关的逻辑"""
|
||||||
@ -153,7 +160,16 @@ class Browser(object):
|
|||||||
:param tab_id: 标签页id
|
:param tab_id: 标签页id
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
self.run_cdp('Target.closeTarget', targetId=tab_id, _ignore=PageDisconnectedError)
|
self._onTargetDestroyed(targetId=tab_id)
|
||||||
|
self.driver.run('Target.closeTarget', targetId=tab_id)
|
||||||
|
|
||||||
|
def stop_driver(self, driver):
|
||||||
|
"""停止一个Driver
|
||||||
|
:param driver: Driver对象
|
||||||
|
:return: None
|
||||||
|
"""
|
||||||
|
driver.stop()
|
||||||
|
self._all_drivers.get(driver.id, set()).discard(driver)
|
||||||
|
|
||||||
def activate_tab(self, tab_id):
|
def activate_tab(self, tab_id):
|
||||||
"""使标签页变为活动状态
|
"""使标签页变为活动状态
|
||||||
@ -175,10 +191,15 @@ class Browser(object):
|
|||||||
:param force: 是否立刻强制终止进程
|
:param force: 是否立刻强制终止进程
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
|
for tab in self._all_drivers.values():
|
||||||
|
for driver in tab:
|
||||||
|
driver.stop()
|
||||||
try:
|
try:
|
||||||
self.run_cdp('Browser.close')
|
self.run_cdp('Browser.close')
|
||||||
except PageDisconnectedError:
|
except PageDisconnectedError:
|
||||||
|
self.driver.stop()
|
||||||
return
|
return
|
||||||
|
self.driver.stop()
|
||||||
|
|
||||||
if force:
|
if force:
|
||||||
ip, port = self.address.split(':')
|
ip, port = self.address.split(':')
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
@Copyright: (c) 2024 by g1879, Inc. All Rights Reserved.
|
@Copyright: (c) 2024 by g1879, Inc. All Rights Reserved.
|
||||||
@License : BSD 3-Clause.
|
@License : BSD 3-Clause.
|
||||||
"""
|
"""
|
||||||
from typing import List, Optional, Union
|
from typing import List, Optional, Union, Set, Dict
|
||||||
|
|
||||||
from .driver import BrowserDriver, Driver
|
from .driver import BrowserDriver, Driver
|
||||||
from .._pages.chromium_page import ChromiumPage
|
from .._pages.chromium_page import ChromiumPage
|
||||||
@ -19,7 +19,8 @@ class Browser(object):
|
|||||||
id: str = ...
|
id: str = ...
|
||||||
address: str = ...
|
address: str = ...
|
||||||
_frames: dict = ...
|
_frames: dict = ...
|
||||||
_drivers: dict = ...
|
_drivers: Dict[str, Driver] = ...
|
||||||
|
_all_drivers: Dict[str, Set[Driver]] = ...
|
||||||
_process_id: Optional[int] = ...
|
_process_id: Optional[int] = ...
|
||||||
_dl_mgr: DownloadManager = ...
|
_dl_mgr: DownloadManager = ...
|
||||||
_connected: bool = ...
|
_connected: bool = ...
|
||||||
@ -49,6 +50,8 @@ class Browser(object):
|
|||||||
|
|
||||||
def close_tab(self, tab_id: str) -> None: ...
|
def close_tab(self, tab_id: str) -> None: ...
|
||||||
|
|
||||||
|
def stop_driver(self, driver: Driver) -> None: ...
|
||||||
|
|
||||||
def activate_tab(self, tab_id: str) -> None: ...
|
def activate_tab(self, tab_id: str) -> None: ...
|
||||||
|
|
||||||
def get_window_bounds(self, tab_id: str = None) -> dict: ...
|
def get_window_bounds(self, tab_id: str = None) -> dict: ...
|
||||||
|
@ -27,6 +27,7 @@ class SessionElement(DrissionElement):
|
|||||||
"""
|
"""
|
||||||
super().__init__(page)
|
super().__init__(page)
|
||||||
self._inner_ele = ele
|
self._inner_ele = ele
|
||||||
|
self._type = 'SessionElement'
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def inner_ele(self):
|
def inner_ele(self):
|
||||||
|
@ -827,7 +827,16 @@ class ChromiumBase(BasePage):
|
|||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
"""断开与页面的连接,不关闭页面"""
|
"""断开与页面的连接,不关闭页面"""
|
||||||
if self._driver:
|
if self._driver:
|
||||||
self.driver.stop()
|
self.browser.stop_driver(self._driver)
|
||||||
|
|
||||||
|
def reconnect(self, wait=0):
|
||||||
|
"""断开与页面原来的页面,重新建立连接
|
||||||
|
:param wait: 断开后等待若干秒再连接
|
||||||
|
:return: None
|
||||||
|
"""
|
||||||
|
self.disconnect()
|
||||||
|
sleep(wait)
|
||||||
|
self._driver = self.browser._get_driver(self._target_id, self)
|
||||||
|
|
||||||
def handle_alert(self, accept=True, send=None, timeout=None, next_one=False):
|
def handle_alert(self, accept=True, send=None, timeout=None, next_one=False):
|
||||||
r = self._handle_alert(accept=accept, send=send, timeout=timeout, next_one=next_one)
|
r = self._handle_alert(accept=accept, send=send, timeout=timeout, next_one=next_one)
|
||||||
|
@ -244,6 +244,8 @@ class ChromiumBase(BasePage):
|
|||||||
|
|
||||||
def disconnect(self) -> None: ...
|
def disconnect(self) -> None: ...
|
||||||
|
|
||||||
|
def reconnect(self, wait: float = 0) -> None: ...
|
||||||
|
|
||||||
def handle_alert(self, accept: bool = True, send: str = None, timeout: float = None,
|
def handle_alert(self, accept: bool = True, send: str = None, timeout: float = None,
|
||||||
next_one: bool = False) -> Union[str, False]: ...
|
next_one: bool = False) -> Union[str, False]: ...
|
||||||
|
|
||||||
|
@ -203,7 +203,7 @@ class ChromiumPage(ChromiumBase):
|
|||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
"""关闭Page管理的标签页"""
|
"""关闭Page管理的标签页"""
|
||||||
self.browser.close_tab(self.tab_id)
|
self.close_tabs(self.tab_id)
|
||||||
|
|
||||||
def close_tabs(self, tabs_or_ids=None, others=False):
|
def close_tabs(self, tabs_or_ids=None, others=False):
|
||||||
"""关闭传入的标签页,默认关闭当前页。可传入多个
|
"""关闭传入的标签页,默认关闭当前页。可传入多个
|
||||||
|
2
setup.py
2
setup.py
@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="DrissionPage",
|
name="DrissionPage",
|
||||||
version="4.0.3",
|
version="4.0.3.1",
|
||||||
author="g1879",
|
author="g1879",
|
||||||
author_email="g1879@qq.com",
|
author_email="g1879@qq.com",
|
||||||
description="Python based web automation tool. It can control the browser and send and receive data packets.",
|
description="Python based web automation tool. It can control the browser and send and receive data packets.",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user