diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index 48bcbf9..d0ddee4 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -170,7 +170,7 @@ class ChromiumPage(ChromiumBase): :param others: 是否关闭指定标签页之外的 :return: None """ - self.browser.close_tabs(tabs_or_ids=tabs_or_ids, others=others) + self.browser.close_tabs(tabs_or_ids=tabs_or_ids or self.tab_id, others=others) def quit(self, timeout=5, force=True): """关闭浏览器 diff --git a/DrissionPage/_units/scroller.py b/DrissionPage/_units/scroller.py index bc5f334..84e6e04 100644 --- a/DrissionPage/_units/scroller.py +++ b/DrissionPage/_units/scroller.py @@ -11,38 +11,43 @@ from time import sleep, perf_counter class Scroller(object): """用于滚动的对象""" - def __init__(self, ele): + def __init__(self, owner): """ - :param ele: 元素对象 + :param owner: 元素对象 """ - self._driver = ele + self._owner = owner self._t1 = self._t2 = 'this' self._wait_complete = False def _run_js(self, js): js = js.format(self._t1, self._t2, self._t2) - self._driver._run_js(js) + self._owner._run_js(js) self._wait_scrolled() def to_top(self): """滚动到顶端,水平位置不变""" self._run_js('{}.scrollTo({}.scrollLeft, 0);') + return self._owner def to_bottom(self): """滚动到底端,水平位置不变""" self._run_js('{}.scrollTo({}.scrollLeft, {}.scrollHeight);') + return self._owner def to_half(self): """滚动到垂直中间位置,水平位置不变""" self._run_js('{}.scrollTo({}.scrollLeft, {}.scrollHeight/2);') + return self._owner def to_rightmost(self): """滚动到最右边,垂直位置不变""" self._run_js('{}.scrollTo({}.scrollWidth, {}.scrollTop);') + return self._owner def to_leftmost(self): """滚动到最左边,垂直位置不变""" self._run_js('{}.scrollTo(0, {}.scrollTop);') + return self._owner def to_location(self, x, y): """滚动到指定位置 @@ -51,6 +56,7 @@ class Scroller(object): :return: None """ self._run_js(f'{{}}.scrollTo({x}, {y});') + return self._owner def up(self, pixel=300): """向上滚动若干像素,水平位置不变 @@ -59,6 +65,7 @@ class Scroller(object): """ pixel = -pixel self._run_js(f'{{}}.scrollBy(0, {pixel});') + return self._owner def down(self, pixel=300): """向下滚动若干像素,水平位置不变 @@ -66,6 +73,7 @@ class Scroller(object): :return: None """ self._run_js(f'{{}}.scrollBy(0, {pixel});') + return self._owner def left(self, pixel=300): """向左滚动若干像素,垂直位置不变 @@ -74,6 +82,7 @@ class Scroller(object): """ pixel = -pixel self._run_js(f'{{}}.scrollBy({pixel}, 0);') + return self._owner def right(self, pixel=300): """向右滚动若干像素,垂直位置不变 @@ -81,13 +90,14 @@ class Scroller(object): :return: None """ self._run_js(f'{{}}.scrollBy({pixel}, 0);') + return self._owner def _wait_scrolled(self): """等待滚动结束""" if not self._wait_complete: return - owner = self._driver.owner if self._driver._type == 'ChromiumElement' else self._driver + owner = self._owner.owner if self._owner._type == 'ChromiumElement' else self._owner r = owner._run_cdp('Page.getLayoutMetrics') x = r['layoutViewport']['pageX'] y = r['layoutViewport']['pageY'] @@ -112,11 +122,13 @@ class ElementScroller(Scroller): :param center: 是否尽量滚动到页面正中,为None时如果被遮挡,则滚动到页面正中 :return: None """ - self._driver.owner.scroll.to_see(self._driver, center=center) + self._owner.owner.scroll.to_see(self._owner, center=center) + return self._owner def to_center(self): """元素尽量滚动到视口中间""" - self._driver.owner.scroll.to_see(self._driver, center=True) + self._owner.owner.scroll.to_see(self._owner, center=True) + return self._owner class PageScroller(Scroller): @@ -134,8 +146,9 @@ class PageScroller(Scroller): :param center: 是否尽量滚动到页面正中,为None时如果被遮挡,则滚动到页面正中 :return: None """ - ele = self._driver._ele(loc_or_ele) + ele = self._owner._ele(loc_or_ele) self._to_see(ele, center) + return self._owner def _to_see(self, ele, center): """执行滚动页面直到元素可见 @@ -173,5 +186,6 @@ class FrameScroller(PageScroller): :param center: 是否尽量滚动到页面正中,为None时如果被遮挡,则滚动到页面正中 :return: None """ - ele = self._driver._ele(loc_or_ele) + ele = self._owner._ele(loc_or_ele) self._to_see(ele, center) + return self._owner diff --git a/DrissionPage/_units/scroller.pyi b/DrissionPage/_units/scroller.pyi index e48c976..4aab16b 100644 --- a/DrissionPage/_units/scroller.pyi +++ b/DrissionPage/_units/scroller.pyi @@ -9,14 +9,19 @@ from typing import Union 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.mix_page import MixPage +from .._pages.tabs import ChromiumTab, MixTab class Scroller(object): - def __init__(self, page_or_ele: Union[ChromiumBase, ChromiumElement]): - self._t1: str = ... - self._t2: str = ... - self._driver: Union[ChromiumBase, ChromiumElement] = ... - self._wait_complete: bool = ... + _t1: str = ... + _t2: str = ... + _owner: Union[ChromiumBase, ChromiumElement] = ... + _wait_complete: bool = ... + + def __init__(self, owner: Union[ChromiumBase, ChromiumElement]): ... def _run_js(self, js: str): ... @@ -45,33 +50,82 @@ class Scroller(object): class ElementScroller(Scroller): - def to_see(self, center: Union[bool, None] = None) -> None: ... + def to_see(self, center: Union[bool, None] = None) -> ChromiumElement: ... - def to_center(self) -> None: ... + def to_center(self) -> ChromiumElement: ... + + def to_top(self) -> ChromiumElement: ... + + def to_bottom(self) -> ChromiumElement: ... + + def to_half(self) -> ChromiumElement: ... + + def to_rightmost(self) -> ChromiumElement: ... + + def to_leftmost(self) -> ChromiumElement: ... + + def to_location(self, x: int, y: int) -> ChromiumElement: ... + + def up(self, pixel: int = 300) -> ChromiumElement: ... + + def down(self, pixel: int = 300) -> ChromiumElement: ... + + def left(self, pixel: int = 300) -> ChromiumElement: ... + + def right(self, pixel: int = 300) -> ChromiumElement: ... class PageScroller(Scroller): - def __init__(self, owner: ChromiumBase): ... + def __init__(self, owner: Union[ChromiumBase, ChromiumElement]): ... - def to_see(self, loc_or_ele: Union[str, tuple, ChromiumElement], center: Union[bool, None] = None) -> None: ... + def to_see(self, + loc_or_ele: Union[str, tuple, ChromiumElement], + center: Union[bool, None] = None) -> Union[ChromiumTab, MixTab, ChromiumPage, MixPage]: ... + + def to_top(self) -> Union[ChromiumTab, MixTab, ChromiumPage, MixPage]: ... + + def to_bottom(self) -> Union[ChromiumTab, MixTab, ChromiumPage, MixPage]: ... + + def to_half(self) -> Union[ChromiumTab, MixTab, ChromiumPage, MixPage]: ... + + def to_rightmost(self) -> Union[ChromiumTab, MixTab, ChromiumPage, MixPage]: ... + + def to_leftmost(self) -> Union[ChromiumTab, MixTab, ChromiumPage, MixPage]: ... + + def to_location(self, x: int, y: int) -> Union[ChromiumTab, MixTab, ChromiumPage, MixPage]: ... + + def up(self, pixel: int = 300) -> Union[ChromiumTab, MixTab, ChromiumPage, MixPage]: ... + + def down(self, pixel: int = 300) -> Union[ChromiumTab, MixTab, ChromiumPage, MixPage]: ... + + def left(self, pixel: int = 300) -> Union[ChromiumTab, MixTab, ChromiumPage, MixPage]: ... + + def right(self, pixel: int = 300) -> Union[ChromiumTab, MixTab, ChromiumPage, MixPage]: ... def _to_see(self, ele: ChromiumElement, center: Union[bool, None]) -> None: ... class FrameScroller(PageScroller): - def __init__(self, frame): - """ - :param frame: ChromiumFrame对象 - """ - self._driver = frame.doc_ele - self._t1 = self._t2 = 'this.documentElement' - self._wait_complete = False + def __init__(self, frame: ChromiumFrame): ... - def to_see(self, loc_or_ele, center=None): - """滚动页面直到元素可见 - :param loc_or_ele: 元素的定位信息,可以是loc元组,或查询字符串 - :param center: 是否尽量滚动到页面正中,为None时如果被遮挡,则滚动到页面正中 - :return: None - """ - ele = loc_or_ele if isinstance(loc_or_ele, ChromiumElement) else self._driver._ele(loc_or_ele) - self._to_see(ele, center) + def to_top(self) -> ChromiumFrame: ... + + def to_bottom(self) -> ChromiumFrame: ... + + def to_half(self) -> ChromiumFrame: ... + + def to_rightmost(self) -> ChromiumFrame: ... + + def to_leftmost(self) -> ChromiumFrame: ... + + def to_location(self, x: int, y: int) -> ChromiumFrame: ... + + def up(self, pixel: int = 300) -> ChromiumFrame: ... + + def down(self, pixel: int = 300) -> ChromiumFrame: ... + + def left(self, pixel: int = 300) -> ChromiumFrame: ... + + def right(self, pixel: int = 300) -> ChromiumFrame: ... + + def to_see(self, loc_or_ele, center=None) -> ChromiumFrame: ...