显示等待
1、导包:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
2、使用技巧
wait = WebDriverWait(self.driver, 5)
wait.until(ec.text_to_be_present_in_element((by.XPATH, '//*[@id="hasContentDiv"]/div[1]/h2'), '章节列表'))
3、源码解析
class WebDriverWait(Generic[D]):
def __init__(
self,
driver: D,
timeout: float, #超时时长
poll_frequency: float = POLL_FREQUENCY, #轮询时间间隔
ignored_exceptions: typing.Optional[WaitExcTypes] = None, #忽略异常
):
def until(self, method: Callable[[D], Union[Literal[False], 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[[D], T], message: str = "") -> Union[T, Literal[True]]:
...
until_not方法功能与until相反。未找到element或者返回false时循环结束。
4、until方法入参中常用的method
title_is(title: str)-> Callable[[WebDriver], bool]:标题是某内容
title_contains(title: str)-> Callable[[WebDriver], bool]:标题包含某内容
presence_of_element_located(locator: Tuple[str, str])-> Callable[[WebDriverOrWebElement], WebElement]:元素加载出,传入定位元组,如(By.ID, 'p')
url_contains(url: str) -> Callable[[WebDriver], bool]:当前url是否包含某个字段
url_matches(pattern: str) -> Callable[[WebDriver], bool]:当前url是否包含传入的pattern
url_to_be(url: str) -> Callable[[WebDriver], bool]:当前url是否为传入字段
url_changes(url: str) -> Callable[[WebDriver], bool]:当前url是否不等于传入字段
visibility_of_element_located(
locator: Tuple[str, str]
) -> Callable[[WebDriverOrWebElement], Union[Literal[False], WebElement]]:元素可见,传入定位元组
visibility_of(element: WebElement) -> Callable[[Any], Union[Literal[False], WebElement]]:可见,传入元素对象
presence_of_all_elements_located(locator: Tuple[str, str]) -> Callable[[WebDriverOrWebElement], List[WebElement]]:所有元素加载出
visibility_of_any_elements_located(locator: Tuple[str, str]) -> Callable[[WebDriverOrWebElement], List[WebElement]]:判断页面至少有一个元素可见 visible,传入locator,一旦定位就返回 the list of located WebElements;不可见(元素隐藏 或是 完全不存在,一个都没有)返回的是 空列表;
和显式等待结合后, 符合 最少存在一个WebElement的 返回符合定位元素条件WebElement的列表,不可见(元素隐藏 或是 完全不存在的)显式等待+报错;
visibility_of_all_elements_located(
locator: Tuple[str, str]
) -> Callable[[WebDriverOrWebElement], Union[List[WebElement], Literal[False]]]:判断页面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[str, str], attribute_: str, text_: str
) -> Callable[[WebDriverOrWebElement], bool]:某个元素属性包含某文字
frame_to_be_available_and_switch_to_it(locator: Union[Tuple[str, str], str]) -> Callable[[WebDriver], bool]:frame加载并切换
invisibility_of_element_located(
locator: Union[WebElement, Tuple[str, str]]
) -> Callable[[WebDriverOrWebElement], Union[WebElement, bool]]:元素是否不可见
invisibility_of_element(
element: Union[WebElement, Tuple[str, str]]
) -> Callable[[WebDriverOrWebElement], Union[WebElement, bool]]:元素是否不可见
element_to_be_clickable(
mark: Union[WebElement, Tuple[str, str]]
) -> Callable[[WebDriverOrWebElement], Union[Literal[False], WebElement]]:元素是否可点击
staleness_of(element: WebElement) -> Callable[[Any], bool]:判断一个元素是否仍在DOM,可判断页面是否已经刷新
element_to_be_selected(element: WebElement) -> Callable[[Any], bool]:元素是否可选择,传元素对象
element_located_to_be_selected(locator: Tuple[str, str]) -> Callable[[WebDriverOrWebElement], bool]:元素可选择,传入定位元组
element_selection_state_to_be(element: WebElement, is_selected: bool) -> Callable[[Any], bool]:传入元素对象以及状态,相等返回True,否则返回False
element_located_selection_state_to_be(
locator: Tuple[str, str], is_selected: bool
) -> Callable[[WebDriverOrWebElement], bool]:传入定位元组以及状态,相等返回True,否则返回False
number_of_windows_to_be(num_windows: int) -> Callable[[WebDriver], bool]:特定窗口数和实际窗口数是否一致
new_window_is_opened(current_handles: List[str]) -> Callable[[WebDriver], bool]:新窗口是否打开
alert_is_present() -> Callable[[WebDriver], Union[Alert, Literal[False]]]:是否出现Alert
element_attribute_to_include(locator: Tuple[str, str], attribute_: str) -> Callable[[WebDriverOrWebElement], bool]:是否包含某个属性
any_of(*expected_conditions: Callable[[D], T]) -> Callable[[D], Union[Literal[False], T]]:对多个期望条件中的任何一个为真则为真。
all_of(
*expected_conditions: Callable[[D], Union[T, Literal[False]]]
) -> Callable[[D], Union[List[T], Literal[False]]]:对多个期望条件中的所有条件为真则为真
none_of(*expected_conditions: Callable[[D], Any]) -> Callable[[D], bool]:对多个期望条件中的所有条件为假则为真
5、日常用法
可用作元素查找或者断言动作是否生效
标签:WebElement,显示,Union,element,Callable,bool,str,等待 From: https://www.cnblogs.com/PengHwei/p/18126303