修复Page对象设置下载路径问题;

优化等待新tab逻辑;
接管来自selenium和playwright的浏览器时忽略无头设置;
优化关闭tab逻辑
This commit is contained in:
g1879 2024-10-14 00:01:59 +08:00
parent 6961fa07ff
commit 65d561e079
9 changed files with 37 additions and 16 deletions

View File

@ -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:

View File

@ -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: 标签页idstrTab对象或标签页序号int序号从1开始

View File

@ -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)

View File

@ -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):

View File

@ -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:

View File

@ -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:

View File

@ -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):

View File

@ -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:

View File

@ -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)