mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
input_txt()功能合并到input()里;修改text属性获取方式(待测试)
This commit is contained in:
parent
74b7185563
commit
5e5ca4fa26
@ -6,8 +6,7 @@
|
|||||||
"""
|
"""
|
||||||
from os import sep
|
from os import sep
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from re import sub
|
from time import time, perf_counter
|
||||||
from time import time
|
|
||||||
from typing import Union, List, Any, Tuple
|
from typing import Union, List, Any, Tuple
|
||||||
|
|
||||||
from selenium.common.exceptions import TimeoutException, JavascriptException, InvalidElementStateException
|
from selenium.common.exceptions import TimeoutException, JavascriptException, InvalidElementStateException
|
||||||
@ -81,10 +80,10 @@ class DriverElement(DrissionElement):
|
|||||||
@property
|
@property
|
||||||
def text(self) -> str:
|
def text(self) -> str:
|
||||||
"""返回元素内所有文本"""
|
"""返回元素内所有文本"""
|
||||||
# return format_html(self.inner_ele.get_attribute('innerText'), False)
|
re_str = self.inner_ele.text
|
||||||
re_str = self.inner_ele.get_attribute('innerText')
|
# re_str = self.inner_ele.get_attribute('innerText')
|
||||||
re_str = sub(r'\n{2,}', '\n', re_str)
|
# re_str = sub(r'\n{2,}', '\n', re_str)
|
||||||
re_str = sub(r' {2,}', ' ', re_str)
|
# re_str = sub(r' {2,}', ' ', re_str)
|
||||||
|
|
||||||
return format_html(re_str.strip('\n '), False)
|
return format_html(re_str.strip('\n '), False)
|
||||||
|
|
||||||
@ -282,7 +281,6 @@ class DriverElement(DrissionElement):
|
|||||||
"""
|
"""
|
||||||
if not by_js:
|
if not by_js:
|
||||||
timeout = timeout if timeout is not None else self.page.timeout
|
timeout = timeout if timeout is not None else self.page.timeout
|
||||||
from time import perf_counter
|
|
||||||
t1 = perf_counter()
|
t1 = perf_counter()
|
||||||
while perf_counter() - t1 <= timeout:
|
while perf_counter() - t1 <= timeout:
|
||||||
try:
|
try:
|
||||||
@ -340,37 +338,35 @@ class DriverElement(DrissionElement):
|
|||||||
from selenium.webdriver import ActionChains
|
from selenium.webdriver import ActionChains
|
||||||
ActionChains(self.page.driver).move_to_element_with_offset(self.inner_ele, x, y).context_click().perform()
|
ActionChains(self.page.driver).move_to_element_with_offset(self.inner_ele, x, y).context_click().perform()
|
||||||
|
|
||||||
def input(self, vals: Union[str, tuple], clear: bool = True) -> None:
|
def input(self, vals: Union[str, tuple], clear: bool = True, insure_input: bool = True) -> None:
|
||||||
"""输入文本或组合键,可用于所有场合 \n
|
"""输入文本或组合键,可用于所有场合 \n
|
||||||
:param vals: 文本值或按键组合
|
:param vals: 文本值或按键组合
|
||||||
:param clear: 输入前是否清空文本框
|
:param clear: 输入前是否清空文本框
|
||||||
|
:param insure_input: 确保输入正确,解决文本框有时输入失效的问题,不能用于输入组合键
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
if clear:
|
if not insure_input: # 普通输入
|
||||||
self.clear()
|
|
||||||
|
|
||||||
self.inner_ele.send_keys(*vals)
|
|
||||||
|
|
||||||
def input_txt(self, txt: Union[str, tuple], clear: bool = True) -> None:
|
|
||||||
"""专门用于输入文本框,解决文本框有时输入失效的问题 \n
|
|
||||||
:param txt: 文本值
|
|
||||||
:param clear: 输入前是否清空文本框
|
|
||||||
:return: None
|
|
||||||
"""
|
|
||||||
enter = '\n' if txt.endswith('\n') else None
|
|
||||||
full_txt = txt if clear else f'{self.attr("value")}{txt}'
|
|
||||||
full_txt = full_txt.rstrip('\n')
|
|
||||||
from time import perf_counter
|
|
||||||
|
|
||||||
t1 = perf_counter()
|
|
||||||
while self.attr('value') != full_txt and perf_counter() - t1 > self.page.timeout:
|
|
||||||
if clear:
|
if clear:
|
||||||
self.clear()
|
self.clear()
|
||||||
|
|
||||||
self.inner_ele.send_keys(txt)
|
self.inner_ele.send_keys(*vals)
|
||||||
|
|
||||||
if enter:
|
else: # 确保输入正确
|
||||||
self.inner_ele.send_keys(enter)
|
if not isinstance(vals, str):
|
||||||
|
raise TypeError('insure_input参数生效时只能接收str数据。')
|
||||||
|
enter = '\n' if vals.endswith('\n') else None
|
||||||
|
full_txt = vals if clear else f'{self.attr("value")}{vals}'
|
||||||
|
full_txt = full_txt.rstrip('\n')
|
||||||
|
|
||||||
|
t1 = perf_counter()
|
||||||
|
while self.is_valid() and self.attr('value') != full_txt and perf_counter() - t1 <= self.page.timeout:
|
||||||
|
if clear:
|
||||||
|
self.clear()
|
||||||
|
|
||||||
|
self.inner_ele.send_keys(vals)
|
||||||
|
|
||||||
|
if enter:
|
||||||
|
self.inner_ele.send_keys(enter)
|
||||||
|
|
||||||
def run_script(self, script: str, *args) -> Any:
|
def run_script(self, script: str, *args) -> Any:
|
||||||
"""执行js代码,传入自己为第一个参数 \n
|
"""执行js代码,传入自己为第一个参数 \n
|
||||||
|
Loading…
x
Reference in New Issue
Block a user