mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
调整格式
This commit is contained in:
parent
87c76e9aa6
commit
2292888ead
@ -549,7 +549,7 @@ class DriverElement(DrissionElement):
|
|||||||
def execute_driver_find(page_or_ele,
|
def execute_driver_find(page_or_ele,
|
||||||
loc: Tuple[str, str],
|
loc: Tuple[str, str],
|
||||||
mode: str = 'single',
|
mode: str = 'single',
|
||||||
timeout: float = 10) -> Union[DriverElement, List[DriverElement or str]]:
|
timeout: float = 10) -> Union[DriverElement, List[DriverElement or str], str, None]:
|
||||||
"""执行driver模式元素的查找 \n
|
"""执行driver模式元素的查找 \n
|
||||||
页面查找元素及元素查找下级元素皆使用此方法 \n
|
页面查找元素及元素查找下级元素皆使用此方法 \n
|
||||||
:param page_or_ele: DriverPage对象或DriverElement对象
|
:param page_or_ele: DriverPage对象或DriverElement对象
|
||||||
|
@ -192,6 +192,7 @@ class DriverPage(object):
|
|||||||
"""
|
"""
|
||||||
if not isinstance(loc_or_str, (tuple, str)):
|
if not isinstance(loc_or_str, (tuple, str)):
|
||||||
raise TypeError('Type of loc_or_str can only be tuple or str.')
|
raise TypeError('Type of loc_or_str can only be tuple or str.')
|
||||||
|
|
||||||
return self.ele(loc_or_str, mode='all', timeout=timeout)
|
return self.ele(loc_or_str, mode='all', timeout=timeout)
|
||||||
|
|
||||||
# ----------------以下为独有函数-----------------------
|
# ----------------以下为独有函数-----------------------
|
||||||
@ -213,40 +214,56 @@ class DriverPage(object):
|
|||||||
|
|
||||||
timeout = timeout or self.timeout
|
timeout = timeout or self.timeout
|
||||||
is_ele = False
|
is_ele = False
|
||||||
|
|
||||||
if isinstance(loc_or_ele, DriverElement):
|
if isinstance(loc_or_ele, DriverElement):
|
||||||
loc_or_ele = loc_or_ele.inner_ele
|
loc_or_ele = loc_or_ele.inner_ele
|
||||||
is_ele = True
|
is_ele = True
|
||||||
|
|
||||||
elif isinstance(loc_or_ele, WebElement):
|
elif isinstance(loc_or_ele, WebElement):
|
||||||
is_ele = True
|
is_ele = True
|
||||||
|
|
||||||
elif isinstance(loc_or_ele, str):
|
elif isinstance(loc_or_ele, str):
|
||||||
loc_or_ele = str_to_loc(loc_or_ele)
|
loc_or_ele = str_to_loc(loc_or_ele)
|
||||||
|
|
||||||
elif isinstance(loc_or_ele, tuple):
|
elif isinstance(loc_or_ele, tuple):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise TypeError('The type of loc_or_ele can only be str, tuple, DriverElement, WebElement')
|
raise TypeError('The type of loc_or_ele can only be str, tuple, DriverElement, WebElement')
|
||||||
|
|
||||||
if is_ele: # 当传入参数是元素对象时
|
# 当传入参数是元素对象时
|
||||||
|
if is_ele:
|
||||||
end_time = time() + timeout
|
end_time = time() + timeout
|
||||||
|
|
||||||
while time() < end_time:
|
while time() < end_time:
|
||||||
if mode == 'del':
|
if mode == 'del':
|
||||||
try:
|
try:
|
||||||
loc_or_ele.is_enabled()
|
loc_or_ele.is_enabled()
|
||||||
except:
|
except:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
elif mode == 'display' and loc_or_ele.is_displayed():
|
elif mode == 'display' and loc_or_ele.is_displayed():
|
||||||
return True
|
return True
|
||||||
|
|
||||||
elif mode == 'hidden' and not loc_or_ele.is_displayed():
|
elif mode == 'hidden' and not loc_or_ele.is_displayed():
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
else: # 当传入参数是控制字符串或元组时
|
|
||||||
|
# 当传入参数是控制字符串或元组时
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
if mode == 'del':
|
if mode == 'del':
|
||||||
WebDriverWait(self.driver, timeout).until_not(ec.presence_of_element_located(loc_or_ele))
|
WebDriverWait(self.driver, timeout).until_not(ec.presence_of_element_located(loc_or_ele))
|
||||||
|
|
||||||
elif mode == 'display':
|
elif mode == 'display':
|
||||||
WebDriverWait(self.driver, timeout).until(ec.visibility_of_element_located(loc_or_ele))
|
WebDriverWait(self.driver, timeout).until(ec.visibility_of_element_located(loc_or_ele))
|
||||||
|
|
||||||
elif mode == 'hidden':
|
elif mode == 'hidden':
|
||||||
WebDriverWait(self.driver, timeout).until_not(ec.visibility_of_element_located(loc_or_ele))
|
WebDriverWait(self.driver, timeout).until_not(ec.visibility_of_element_located(loc_or_ele))
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
except:
|
except:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -299,6 +316,7 @@ class DriverPage(object):
|
|||||||
def close_current_tab(self) -> None:
|
def close_current_tab(self) -> None:
|
||||||
"""关闭当前标签页"""
|
"""关闭当前标签页"""
|
||||||
self.driver.close()
|
self.driver.close()
|
||||||
|
|
||||||
if self.tabs_count:
|
if self.tabs_count:
|
||||||
self.to_tab(0)
|
self.to_tab(0)
|
||||||
|
|
||||||
@ -313,6 +331,7 @@ class DriverPage(object):
|
|||||||
tab = num_or_handle
|
tab = num_or_handle
|
||||||
|
|
||||||
tabs = self.driver.window_handles
|
tabs = self.driver.window_handles
|
||||||
|
|
||||||
if tab is None:
|
if tab is None:
|
||||||
page_handle = self.current_tab_handle
|
page_handle = self.current_tab_handle
|
||||||
elif isinstance(tab, int):
|
elif isinstance(tab, int):
|
||||||
@ -326,6 +345,7 @@ class DriverPage(object):
|
|||||||
if i != page_handle:
|
if i != page_handle:
|
||||||
self.driver.switch_to.window(i)
|
self.driver.switch_to.window(i)
|
||||||
self.driver.close()
|
self.driver.close()
|
||||||
|
|
||||||
self.driver.switch_to.window(page_handle) # 把权柄定位回保留的页面
|
self.driver.switch_to.window(page_handle) # 把权柄定位回保留的页面
|
||||||
|
|
||||||
def to_tab(self, num_or_handle: Union[int, str] = 0) -> None:
|
def to_tab(self, num_or_handle: Union[int, str] = 0) -> None:
|
||||||
@ -337,6 +357,7 @@ class DriverPage(object):
|
|||||||
tab = int(num_or_handle)
|
tab = int(num_or_handle)
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
tab = num_or_handle
|
tab = num_or_handle
|
||||||
|
|
||||||
tab = self.driver.window_handles[tab] if isinstance(tab, int) else tab
|
tab = self.driver.window_handles[tab] if isinstance(tab, int) else tab
|
||||||
self.driver.switch_to.window(tab)
|
self.driver.switch_to.window(tab)
|
||||||
|
|
||||||
@ -355,22 +376,34 @@ class DriverPage(object):
|
|||||||
:param loc_or_ele: iframe的定位信息
|
:param loc_or_ele: iframe的定位信息
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
if isinstance(loc_or_ele, int): # 根据序号跳转
|
# 根据序号跳转
|
||||||
|
if isinstance(loc_or_ele, int):
|
||||||
self.driver.switch_to.frame(loc_or_ele)
|
self.driver.switch_to.frame(loc_or_ele)
|
||||||
|
|
||||||
elif isinstance(loc_or_ele, str):
|
elif isinstance(loc_or_ele, str):
|
||||||
if loc_or_ele == 'main': # 跳转到最上级
|
# 跳转到最上级
|
||||||
|
if loc_or_ele == 'main':
|
||||||
self.driver.switch_to.default_content()
|
self.driver.switch_to.default_content()
|
||||||
elif loc_or_ele == 'parent': # 跳转到上一层
|
|
||||||
|
# 跳转到上一层
|
||||||
|
elif loc_or_ele == 'parent':
|
||||||
self.driver.switch_to.parent_frame()
|
self.driver.switch_to.parent_frame()
|
||||||
elif ':' not in loc_or_ele and '=' not in loc_or_ele: # 传入id或name
|
|
||||||
|
# 传入id或name
|
||||||
|
elif ':' not in loc_or_ele and '=' not in loc_or_ele:
|
||||||
self.driver.switch_to.frame(loc_or_ele)
|
self.driver.switch_to.frame(loc_or_ele)
|
||||||
else: # 传入控制字符串
|
|
||||||
|
# 传入控制字符串
|
||||||
|
else:
|
||||||
ele = self.ele(loc_or_ele)
|
ele = self.ele(loc_or_ele)
|
||||||
self.driver.switch_to.frame(ele.inner_ele)
|
self.driver.switch_to.frame(ele.inner_ele)
|
||||||
|
|
||||||
elif isinstance(loc_or_ele, WebElement):
|
elif isinstance(loc_or_ele, WebElement):
|
||||||
self.driver.switch_to.frame(loc_or_ele)
|
self.driver.switch_to.frame(loc_or_ele)
|
||||||
|
|
||||||
elif isinstance(loc_or_ele, DriverElement):
|
elif isinstance(loc_or_ele, DriverElement):
|
||||||
self.driver.switch_to.frame(loc_or_ele.inner_ele)
|
self.driver.switch_to.frame(loc_or_ele.inner_ele)
|
||||||
|
|
||||||
elif isinstance(loc_or_ele, tuple):
|
elif isinstance(loc_or_ele, tuple):
|
||||||
ele = self.ele(loc_or_ele)
|
ele = self.ele(loc_or_ele)
|
||||||
self.driver.switch_to.frame(ele.inner_ele)
|
self.driver.switch_to.frame(ele.inner_ele)
|
||||||
@ -405,21 +438,29 @@ class DriverPage(object):
|
|||||||
"""
|
"""
|
||||||
if mode == 'top':
|
if mode == 'top':
|
||||||
self.driver.execute_script("window.scrollTo(document.documentElement.scrollLeft,0);")
|
self.driver.execute_script("window.scrollTo(document.documentElement.scrollLeft,0);")
|
||||||
|
|
||||||
elif mode == 'bottom':
|
elif mode == 'bottom':
|
||||||
self.driver.execute_script(
|
self.driver.execute_script(
|
||||||
"window.scrollTo(document.documentElement.scrollLeft,document.body.scrollHeight);")
|
"window.scrollTo(document.documentElement.scrollLeft,document.body.scrollHeight);")
|
||||||
|
|
||||||
elif mode == 'rightmost':
|
elif mode == 'rightmost':
|
||||||
self.driver.execute_script("window.scrollTo(document.body.scrollWidth,document.documentElement.scrollTop);")
|
self.driver.execute_script("window.scrollTo(document.body.scrollWidth,document.documentElement.scrollTop);")
|
||||||
|
|
||||||
elif mode == 'leftmost':
|
elif mode == 'leftmost':
|
||||||
self.driver.execute_script("window.scrollTo(0,document.documentElement.scrollTop);")
|
self.driver.execute_script("window.scrollTo(0,document.documentElement.scrollTop);")
|
||||||
|
|
||||||
elif mode == 'up':
|
elif mode == 'up':
|
||||||
self.driver.execute_script(f"window.scrollBy(0,-{pixel});")
|
self.driver.execute_script(f"window.scrollBy(0,-{pixel});")
|
||||||
|
|
||||||
elif mode == 'down':
|
elif mode == 'down':
|
||||||
self.driver.execute_script(f"window.scrollBy(0,{pixel});")
|
self.driver.execute_script(f"window.scrollBy(0,{pixel});")
|
||||||
|
|
||||||
elif mode == 'left':
|
elif mode == 'left':
|
||||||
self.driver.execute_script(f"window.scrollBy(-{pixel},0);")
|
self.driver.execute_script(f"window.scrollBy(-{pixel},0);")
|
||||||
|
|
||||||
elif mode == 'right':
|
elif mode == 'right':
|
||||||
self.driver.execute_script(f"window.scrollBy({pixel},0);")
|
self.driver.execute_script(f"window.scrollBy({pixel},0);")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Argument mode can only be 'top', 'bottom', 'rightmost', 'leftmost', 'up', 'down', 'left', 'right'.")
|
"Argument mode can only be 'top', 'bottom', 'rightmost', 'leftmost', 'up', 'down', 'left', 'right'.")
|
||||||
@ -440,9 +481,11 @@ class DriverPage(object):
|
|||||||
"""
|
"""
|
||||||
if not x and not y:
|
if not x and not y:
|
||||||
self.driver.maximize_window()
|
self.driver.maximize_window()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if x <= 0 or y <= 0:
|
if x <= 0 or y <= 0:
|
||||||
raise ValueError('Arguments x and y must greater than 0.')
|
raise ValueError('Arguments x and y must greater than 0.')
|
||||||
|
|
||||||
new_x = x or self.driver.get_window_size()['width']
|
new_x = x or self.driver.get_window_size()['width']
|
||||||
new_y = y or self.driver.get_window_size()['height']
|
new_y = y or self.driver.get_window_size()['height']
|
||||||
self.driver.set_window_size(new_x, new_y)
|
self.driver.set_window_size(new_x, new_y)
|
||||||
@ -464,11 +507,16 @@ class DriverPage(object):
|
|||||||
alert = self.driver.switch_to.alert
|
alert = self.driver.switch_to.alert
|
||||||
except NoAlertPresentException:
|
except NoAlertPresentException:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if text:
|
if text:
|
||||||
alert.send_keys(text)
|
alert.send_keys(text)
|
||||||
|
|
||||||
text = alert.text
|
text = alert.text
|
||||||
|
|
||||||
if mode == 'cancel':
|
if mode == 'cancel':
|
||||||
alert.dismiss()
|
alert.dismiss()
|
||||||
|
|
||||||
elif mode == 'ok':
|
elif mode == 'ok':
|
||||||
alert.accept()
|
alert.accept()
|
||||||
|
|
||||||
return text
|
return text
|
||||||
|
@ -172,6 +172,7 @@ class SessionPage(object):
|
|||||||
|
|
||||||
print('重试', to_url)
|
print('重试', to_url)
|
||||||
sleep(interval)
|
sleep(interval)
|
||||||
|
|
||||||
r = self._make_response(to_url, mode=mode, show_errmsg=show_errmsg, **kwargs)[0]
|
r = self._make_response(to_url, mode=mode, show_errmsg=show_errmsg, **kwargs)[0]
|
||||||
times -= 1
|
times -= 1
|
||||||
|
|
||||||
@ -248,6 +249,7 @@ class SessionPage(object):
|
|||||||
else:
|
else:
|
||||||
if self._response.ok:
|
if self._response.ok:
|
||||||
self._url_available = True
|
self._url_available = True
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if show_errmsg:
|
if show_errmsg:
|
||||||
raise ConnectionError(f'Status code: {self._response.status_code}.')
|
raise ConnectionError(f'Status code: {self._response.status_code}.')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user