显示等待
显示等待1、导包:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec2、使用技巧
wait = WebDriverWait(self.driver, 5)
wait.until(ec.text_to_be_present_in_element((by.XPATH, '//*[@id="hasContentDiv"]/div/h2'), '章节列表'))3、源码解析
class WebDriverWait(Generic):
def __init__(
self,
driver: D,
timeout: float,#超时时长
poll_frequency: float = POLL_FREQUENCY,#轮询时间间隔
ignored_exceptions: typing.Optional = None, #忽略异常
):
def until(self, method: Callable[, Union, T]], message: str = "") -> T:
"""Calls the method provided with the driver as an argument until the \
return value does not evaluate to ``False``.
:param method: callable(WebDriver)
:param message: optional message for :exc:`TimeoutException`
:returns: the result of the last call to `method`
:raises: :exc:`selenium.common.exceptions.TimeoutException` if timeout occurs
"""
screen = None
stacktrace = None
end_time = time.monotonic() + self._timeout
while True:
try:
value = method(self._driver) # 调用传入的方法,有返回值则正常返回
if value:
return value
except self._ignored_exceptions as exc:
screen = getattr(exc, "screen", None)
stacktrace = getattr(exc, "stacktrace", None)
time.sleep(self._poll) # 出现异常或者无返回值时,等待轮询时间间隔
if time.monotonic() > end_time:
break # 达到超时时长,循环结束
raise TimeoutException(message, screen, stacktrace) # 达到超时时长,抛出异常
until方法传入可调用对象类型method、异常放回字段message。找到element或者返回true时循环结束。
def until_not(self, method: Callable[, T], message: str = "") -> Union]:
...
until_not方法功能与until相反。未找到element或者返回false时循环结束。4、until方法入参中常用的method
title_is(title: str)-> Callable[, bool]:标题是某内容
title_contains(title: str)-> Callable[, bool]:标题包含某内容
presence_of_element_located(locator: Tuple)-> Callable[, WebElement]:元素加载出,传入定位元组,如(By.ID, 'p')
url_contains(url: str) -> Callable[, bool]:当前url是否包含某个字段
url_matches(pattern: str) -> Callable[, bool]:当前url是否包含传入的pattern
url_to_be(url: str) -> Callable[, bool]:当前url是否为传入字段
url_changes(url: str) -> Callable[, bool]:当前url是否不等于传入字段
visibility_of_element_located(
locator: Tuple
) -> Callable[, Union, WebElement]]:元素可见,传入定位元组
visibility_of(element: WebElement) -> Callable[, Union, WebElement]]:可见,传入元素对象
presence_of_all_elements_located(locator: Tuple) -> Callable[, List]:所有元素加载出
visibility_of_any_elements_located(locator: Tuple) -> Callable[, List]:判断页面至少有一个元素可见 visible,传入locator,一旦定位就返回 the list of located WebElements;不可见(元素隐藏 或是 完全不存在,一个都没有)返回的是 空列表;
和显式等待结合后, 符合 最少存在一个WebElement的 返回符合定位元素条件WebElement的列表,不可见(元素隐藏 或是 完全不存在的)显式等待+报错;
visibility_of_all_elements_located(
locator: Tuple
) -> Callable[, Union, Literal]]:判断页面all elements存在且可见 visible
all elements are present and visible;传入locator,全部符合的 就返回 the list of located and visible WebElements;不能全部符合的返回False;不存在的元素返回 空列表; 和显式等待结合后,符合 全部可见WebElement的 返回符合定位元素条件WebElement的列表,找不到元素的 + WebElement不能全部可见的 显式等待+报错
text_to_be_present_in_element:某个元素文本包含某文字
text_to_be_present_in_element_value:某个元素值包含某文字
text_to_be_present_in_element_attribute(
locator: Tuple, attribute_: str, text_: str
) -> Callable[, bool]:某个元素属性包含某文字
frame_to_be_available_and_switch_to_it(locator: Union, str]) -> Callable[, bool]:frame加载并切换
invisibility_of_element_located(
locator: Union]
) -> Callable[, Union]:元素是否不可见
invisibility_of_element(
element: Union]
) -> Callable[, Union]:元素是否不可见
element_to_be_clickable(
mark: Union]
) -> Callable[, Union, WebElement]]:元素是否可点击
staleness_of(element: WebElement) -> Callable[, bool]:判断一个元素是否仍在DOM,可判断页面是否已经刷新
element_to_be_selected(element: WebElement) -> Callable[, bool]:元素是否可选择,传元素对象
element_located_to_be_selected(locator: Tuple) -> Callable[, bool]:元素可选择,传入定位元组
element_selection_state_to_be(element: WebElement, is_selected: bool) -> Callable[, bool]:传入元素对象以及状态,相等返回True,否则返回False
element_located_selection_state_to_be(
locator: Tuple, is_selected: bool
) -> Callable[, bool]:传入定位元组以及状态,相等返回True,否则返回False
number_of_windows_to_be(num_windows: int) -> Callable[, bool]:特定窗口数和实际窗口数是否一致
new_window_is_opened(current_handles: List) -> Callable[, bool]:新窗口是否打开
alert_is_present() -> Callable[, Union]]:是否出现Alert
element_attribute_to_include(locator: Tuple, attribute_: str) -> Callable[, bool]:是否包含某个属性
any_of(*expected_conditions: Callable[, T]) -> Callable[, Union, T]]:对多个期望条件中的任何一个为真则为真。
all_of(
*expected_conditions: Callable[, Union]]
) -> Callable[, Union, Literal]]:对多个期望条件中的所有条件为真则为真
none_of(*expected_conditions: Callable[, Any]) -> Callable[, bool]:对多个期望条件中的所有条件为假则为真5、日常用法
可用作元素查找或者断言动作是否生效
来源:https://www.cnblogs.com/PengHwei/p/18126303
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页:
[1]