ChromiumFrame支持滚动

This commit is contained in:
g1879 2023-01-10 18:35:15 +08:00
parent 9ddbe27b6c
commit 6ced0a0b43
3 changed files with 31 additions and 18 deletions

View File

@ -1300,6 +1300,7 @@ def run_js(page_or_ele, script, as_expr=False, timeout=None, args=None, not_chan
exceptionDetails = res.get('exceptionDetails') exceptionDetails = res.get('exceptionDetails')
if exceptionDetails: if exceptionDetails:
print(script)
raise RuntimeError(f'javascript错误: {exceptionDetails}') raise RuntimeError(f'javascript错误: {exceptionDetails}')
try: try:
@ -1397,41 +1398,41 @@ class ChromiumScroll(object):
def __init__(self, page_or_ele): def __init__(self, page_or_ele):
""" """
:param page_or_ele: ChromePage或ChromiumElement :param page_or_ele: 页面对象元素对象或frame对象
""" """
self.page_or_ele = page_or_ele self._driver = page_or_ele
if isinstance(page_or_ele, ChromiumElement): if isinstance(page_or_ele, ChromiumElement):
self.t1 = self.t2 = 'this' self.t1 = self.t2 = 'this'
elif 'ChromiumFrame' in str(type(page_or_ele)):
self._driver = page_or_ele.doc_ele
self.t1 = self.t2 = 'this.documentElement'
else: else:
self.t1 = 'window' self.t1 = 'window'
self.t2 = 'document.documentElement' self.t2 = 'document.documentElement'
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)
if self.t1 == 'this': # 在元素上滚动 self._driver.run_js(js)
self.page_or_ele.run_js(js)
else:
self.page_or_ele.run_js(js, as_expr=True)
def to_top(self): def to_top(self):
"""滚动到顶端,水平位置不变""" """滚动到顶端,水平位置不变"""
self._run_js('{}.scrollTo({}.scrollLeft,0);') self._run_js('{}.scrollTo({}.scrollLeft, 0);')
def to_bottom(self): def to_bottom(self):
"""滚动到底端,水平位置不变""" """滚动到底端,水平位置不变"""
self._run_js('{}.scrollTo({}.scrollLeft,{}.scrollHeight);') self._run_js('{}.scrollTo({}.scrollLeft, {}.scrollHeight);')
def to_half(self): def to_half(self):
"""滚动到垂直中间位置,水平位置不变""" """滚动到垂直中间位置,水平位置不变"""
self._run_js('{}.scrollTo({}.scrollLeft,{}.scrollHeight/2);') self._run_js('{}.scrollTo({}.scrollLeft, {}.scrollHeight/2);')
def to_rightmost(self): def to_rightmost(self):
"""滚动到最右边,垂直位置不变""" """滚动到最右边,垂直位置不变"""
self._run_js('{}.scrollTo({}.scrollWidth,{}.scrollTop);') self._run_js('{}.scrollTo({}.scrollWidth, {}.scrollTop);')
def to_leftmost(self): def to_leftmost(self):
"""滚动到最左边,垂直位置不变""" """滚动到最左边,垂直位置不变"""
self._run_js('{}.scrollTo(0,{}.scrollTop);') self._run_js('{}.scrollTo(0, {}.scrollTop);')
def to_location(self, x, y): def to_location(self, x, y):
"""滚动到指定位置 \n """滚动到指定位置 \n
@ -1439,7 +1440,7 @@ class ChromiumScroll(object):
:param y: 垂直距离 :param y: 垂直距离
:return: None :return: None
""" """
self._run_js(f'{{}}.scrollTo({x},{y});') self._run_js(f'{{}}.scrollTo({x}, {y});')
def up(self, pixel=300): def up(self, pixel=300):
"""向上滚动若干像素,水平位置不变 \n """向上滚动若干像素,水平位置不变 \n
@ -1447,14 +1448,14 @@ class ChromiumScroll(object):
:return: None :return: None
""" """
pixel = -pixel pixel = -pixel
self._run_js(f'{{}}.scrollBy(0,{pixel});') self._run_js(f'{{}}.scrollBy(0, {pixel});')
def down(self, pixel=300): def down(self, pixel=300):
"""向下滚动若干像素,水平位置不变 \n """向下滚动若干像素,水平位置不变 \n
:param pixel: 滚动的像素 :param pixel: 滚动的像素
:return: None :return: None
""" """
self._run_js(f'{{}}.scrollBy(0,{pixel});') self._run_js(f'{{}}.scrollBy(0, {pixel});')
def left(self, pixel=300): def left(self, pixel=300):
"""向左滚动若干像素,垂直位置不变 \n """向左滚动若干像素,垂直位置不变 \n
@ -1462,14 +1463,14 @@ class ChromiumScroll(object):
:return: None :return: None
""" """
pixel = -pixel pixel = -pixel
self._run_js(f'{{}}.scrollBy({pixel},0);') self._run_js(f'{{}}.scrollBy({pixel}, 0);')
def right(self, pixel=300): def right(self, pixel=300):
"""向右滚动若干像素,垂直位置不变 \n """向右滚动若干像素,垂直位置不变 \n
:param pixel: 滚动的像素 :param pixel: 滚动的像素
:return: None :return: None
""" """
self._run_js(f'{{}}.scrollBy({pixel},0);') self._run_js(f'{{}}.scrollBy({pixel}, 0);')
class ChromiumSelect(object): class ChromiumSelect(object):

View File

@ -381,10 +381,10 @@ def _send_key(ele: ChromiumElement, modifier: int, key: str) -> None: ...
class ChromiumScroll(object): class ChromiumScroll(object):
def __init__(self, page_or_ele: Union[ChromiumBase, ChromiumElement]): def __init__(self, page_or_ele: Union[ChromiumBase, ChromiumElement, ChromiumFrame]):
self.t1: str = ... self.t1: str = ...
self.t2: str = ... self.t2: str = ...
self.page_or_ele: Union[ChromiumPage, ChromiumElement] = ... self._driver: Union[ChromiumPage, ChromiumElement, ChromiumFrame] = ...
def _run_js(self, js: str): ... def _run_js(self, js: str): ...

View File

@ -256,3 +256,15 @@ iframe.refresh()
- `*args`:参数,按顺序在 js 文本中对应 argument[0]、argument[1]... - `*args`:参数,按顺序在 js 文本中对应 argument[0]、argument[1]...
**返回:** 运行的结果 **返回:** 运行的结果
## 📍 `scroll`
`ChromiumFrame`的滚动方法与页面或元素是一致的。
**示例:**
使`<iframe>`元素向下滚动 300 像素
```python
iframe.scroll.down(300)
```