mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
滚动返回调用者;ChromiumPage的close_tabs()默认指定自己
This commit is contained in:
parent
7c7ecc866c
commit
0d6a8b67f2
@ -170,7 +170,7 @@ class ChromiumPage(ChromiumBase):
|
|||||||
:param others: 是否关闭指定标签页之外的
|
:param others: 是否关闭指定标签页之外的
|
||||||
:return: None
|
: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):
|
def quit(self, timeout=5, force=True):
|
||||||
"""关闭浏览器
|
"""关闭浏览器
|
||||||
|
@ -11,38 +11,43 @@ from time import sleep, perf_counter
|
|||||||
class Scroller(object):
|
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._t1 = self._t2 = 'this'
|
||||||
self._wait_complete = False
|
self._wait_complete = False
|
||||||
|
|
||||||
def _run_js(self, js):
|
def _run_js(self, js):
|
||||||
js = js.format(self._t1, self._t2, self._t2)
|
js = js.format(self._t1, self._t2, self._t2)
|
||||||
self._driver._run_js(js)
|
self._owner._run_js(js)
|
||||||
self._wait_scrolled()
|
self._wait_scrolled()
|
||||||
|
|
||||||
def to_top(self):
|
def to_top(self):
|
||||||
"""滚动到顶端,水平位置不变"""
|
"""滚动到顶端,水平位置不变"""
|
||||||
self._run_js('{}.scrollTo({}.scrollLeft, 0);')
|
self._run_js('{}.scrollTo({}.scrollLeft, 0);')
|
||||||
|
return self._owner
|
||||||
|
|
||||||
def to_bottom(self):
|
def to_bottom(self):
|
||||||
"""滚动到底端,水平位置不变"""
|
"""滚动到底端,水平位置不变"""
|
||||||
self._run_js('{}.scrollTo({}.scrollLeft, {}.scrollHeight);')
|
self._run_js('{}.scrollTo({}.scrollLeft, {}.scrollHeight);')
|
||||||
|
return self._owner
|
||||||
|
|
||||||
def to_half(self):
|
def to_half(self):
|
||||||
"""滚动到垂直中间位置,水平位置不变"""
|
"""滚动到垂直中间位置,水平位置不变"""
|
||||||
self._run_js('{}.scrollTo({}.scrollLeft, {}.scrollHeight/2);')
|
self._run_js('{}.scrollTo({}.scrollLeft, {}.scrollHeight/2);')
|
||||||
|
return self._owner
|
||||||
|
|
||||||
def to_rightmost(self):
|
def to_rightmost(self):
|
||||||
"""滚动到最右边,垂直位置不变"""
|
"""滚动到最右边,垂直位置不变"""
|
||||||
self._run_js('{}.scrollTo({}.scrollWidth, {}.scrollTop);')
|
self._run_js('{}.scrollTo({}.scrollWidth, {}.scrollTop);')
|
||||||
|
return self._owner
|
||||||
|
|
||||||
def to_leftmost(self):
|
def to_leftmost(self):
|
||||||
"""滚动到最左边,垂直位置不变"""
|
"""滚动到最左边,垂直位置不变"""
|
||||||
self._run_js('{}.scrollTo(0, {}.scrollTop);')
|
self._run_js('{}.scrollTo(0, {}.scrollTop);')
|
||||||
|
return self._owner
|
||||||
|
|
||||||
def to_location(self, x, y):
|
def to_location(self, x, y):
|
||||||
"""滚动到指定位置
|
"""滚动到指定位置
|
||||||
@ -51,6 +56,7 @@ class Scroller(object):
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
self._run_js(f'{{}}.scrollTo({x}, {y});')
|
self._run_js(f'{{}}.scrollTo({x}, {y});')
|
||||||
|
return self._owner
|
||||||
|
|
||||||
def up(self, pixel=300):
|
def up(self, pixel=300):
|
||||||
"""向上滚动若干像素,水平位置不变
|
"""向上滚动若干像素,水平位置不变
|
||||||
@ -59,6 +65,7 @@ class Scroller(object):
|
|||||||
"""
|
"""
|
||||||
pixel = -pixel
|
pixel = -pixel
|
||||||
self._run_js(f'{{}}.scrollBy(0, {pixel});')
|
self._run_js(f'{{}}.scrollBy(0, {pixel});')
|
||||||
|
return self._owner
|
||||||
|
|
||||||
def down(self, pixel=300):
|
def down(self, pixel=300):
|
||||||
"""向下滚动若干像素,水平位置不变
|
"""向下滚动若干像素,水平位置不变
|
||||||
@ -66,6 +73,7 @@ class Scroller(object):
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
self._run_js(f'{{}}.scrollBy(0, {pixel});')
|
self._run_js(f'{{}}.scrollBy(0, {pixel});')
|
||||||
|
return self._owner
|
||||||
|
|
||||||
def left(self, pixel=300):
|
def left(self, pixel=300):
|
||||||
"""向左滚动若干像素,垂直位置不变
|
"""向左滚动若干像素,垂直位置不变
|
||||||
@ -74,6 +82,7 @@ class Scroller(object):
|
|||||||
"""
|
"""
|
||||||
pixel = -pixel
|
pixel = -pixel
|
||||||
self._run_js(f'{{}}.scrollBy({pixel}, 0);')
|
self._run_js(f'{{}}.scrollBy({pixel}, 0);')
|
||||||
|
return self._owner
|
||||||
|
|
||||||
def right(self, pixel=300):
|
def right(self, pixel=300):
|
||||||
"""向右滚动若干像素,垂直位置不变
|
"""向右滚动若干像素,垂直位置不变
|
||||||
@ -81,13 +90,14 @@ class Scroller(object):
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
self._run_js(f'{{}}.scrollBy({pixel}, 0);')
|
self._run_js(f'{{}}.scrollBy({pixel}, 0);')
|
||||||
|
return self._owner
|
||||||
|
|
||||||
def _wait_scrolled(self):
|
def _wait_scrolled(self):
|
||||||
"""等待滚动结束"""
|
"""等待滚动结束"""
|
||||||
if not self._wait_complete:
|
if not self._wait_complete:
|
||||||
return
|
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')
|
r = owner._run_cdp('Page.getLayoutMetrics')
|
||||||
x = r['layoutViewport']['pageX']
|
x = r['layoutViewport']['pageX']
|
||||||
y = r['layoutViewport']['pageY']
|
y = r['layoutViewport']['pageY']
|
||||||
@ -112,11 +122,13 @@ class ElementScroller(Scroller):
|
|||||||
:param center: 是否尽量滚动到页面正中,为None时如果被遮挡,则滚动到页面正中
|
:param center: 是否尽量滚动到页面正中,为None时如果被遮挡,则滚动到页面正中
|
||||||
:return: 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):
|
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):
|
class PageScroller(Scroller):
|
||||||
@ -134,8 +146,9 @@ class PageScroller(Scroller):
|
|||||||
:param center: 是否尽量滚动到页面正中,为None时如果被遮挡,则滚动到页面正中
|
:param center: 是否尽量滚动到页面正中,为None时如果被遮挡,则滚动到页面正中
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
ele = self._driver._ele(loc_or_ele)
|
ele = self._owner._ele(loc_or_ele)
|
||||||
self._to_see(ele, center)
|
self._to_see(ele, center)
|
||||||
|
return self._owner
|
||||||
|
|
||||||
def _to_see(self, ele, center):
|
def _to_see(self, ele, center):
|
||||||
"""执行滚动页面直到元素可见
|
"""执行滚动页面直到元素可见
|
||||||
@ -173,5 +186,6 @@ class FrameScroller(PageScroller):
|
|||||||
:param center: 是否尽量滚动到页面正中,为None时如果被遮挡,则滚动到页面正中
|
:param center: 是否尽量滚动到页面正中,为None时如果被遮挡,则滚动到页面正中
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
ele = self._driver._ele(loc_or_ele)
|
ele = self._owner._ele(loc_or_ele)
|
||||||
self._to_see(ele, center)
|
self._to_see(ele, center)
|
||||||
|
return self._owner
|
||||||
|
@ -9,14 +9,19 @@ from typing import Union
|
|||||||
|
|
||||||
from .._elements.chromium_element import ChromiumElement
|
from .._elements.chromium_element import ChromiumElement
|
||||||
from .._pages.chromium_base import ChromiumBase
|
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):
|
class Scroller(object):
|
||||||
def __init__(self, page_or_ele: Union[ChromiumBase, ChromiumElement]):
|
_t1: str = ...
|
||||||
self._t1: str = ...
|
_t2: str = ...
|
||||||
self._t2: str = ...
|
_owner: Union[ChromiumBase, ChromiumElement] = ...
|
||||||
self._driver: Union[ChromiumBase, ChromiumElement] = ...
|
_wait_complete: bool = ...
|
||||||
self._wait_complete: bool = ...
|
|
||||||
|
def __init__(self, owner: Union[ChromiumBase, ChromiumElement]): ...
|
||||||
|
|
||||||
def _run_js(self, js: str): ...
|
def _run_js(self, js: str): ...
|
||||||
|
|
||||||
@ -45,33 +50,82 @@ class Scroller(object):
|
|||||||
|
|
||||||
class ElementScroller(Scroller):
|
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):
|
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: ...
|
def _to_see(self, ele: ChromiumElement, center: Union[bool, None]) -> None: ...
|
||||||
|
|
||||||
|
|
||||||
class FrameScroller(PageScroller):
|
class FrameScroller(PageScroller):
|
||||||
def __init__(self, frame):
|
def __init__(self, frame: ChromiumFrame): ...
|
||||||
"""
|
|
||||||
:param frame: ChromiumFrame对象
|
|
||||||
"""
|
|
||||||
self._driver = frame.doc_ele
|
|
||||||
self._t1 = self._t2 = 'this.documentElement'
|
|
||||||
self._wait_complete = False
|
|
||||||
|
|
||||||
def to_see(self, loc_or_ele, center=None):
|
def to_top(self) -> ChromiumFrame: ...
|
||||||
"""滚动页面直到元素可见
|
|
||||||
:param loc_or_ele: 元素的定位信息,可以是loc元组,或查询字符串
|
def to_bottom(self) -> ChromiumFrame: ...
|
||||||
:param center: 是否尽量滚动到页面正中,为None时如果被遮挡,则滚动到页面正中
|
|
||||||
:return: None
|
def to_half(self) -> ChromiumFrame: ...
|
||||||
"""
|
|
||||||
ele = loc_or_ele if isinstance(loc_or_ele, ChromiumElement) else self._driver._ele(loc_or_ele)
|
def to_rightmost(self) -> ChromiumFrame: ...
|
||||||
self._to_see(ele, center)
|
|
||||||
|
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: ...
|
||||||
|
Loading…
x
Reference in New Issue
Block a user