Selenium 中ExpectedConditions 用法说明(最全整理)

By | November 10, 2020

首先导入包

from selenium.webdriver.support import expected_conditions as EC

1,判断当前页面的title是否精确等于预期

titleIs( String title)
EC.title_is('Google')

2,判断当前页面的title是否包含预期字符串

titleContains( String title)
if EC.title_contains('google'):  
##等于
if 'google' in driver.title:

3,判断当前页面的url是否精确等于预期

urlToBe( String url)

4,判断当前页面的url是否包含预期字符串

urlContains( String fraction)

5,当前URL字符串正则表达式匹配

urlMatches( String regex)

6,判断元素是否出现,只要有一个元素出现,就通过。(出现不代表可见
判断是否至少有 1 个元素存在于 dom 树中。举个例子,如果页面上有 n 个元素的 class 都是’column-md-3’,那么只要有 1 个元素存在,这个方法就返回 True。

presenceOfElementLocated( By locator)

7,判断元素是否出现,必须所有符合条件的元素都加载出来,才通过。

presenceOfElementsLocated( By locator)

8,如果给定元素是可见的且具有非零大小,否则为null

elementIfVisible(WebElement element)

9,判断元素是否出现。

presenceOfAllElementsLocatedBy( By locator)

10,判断某个元素是否可见. 可见代表元素非隐藏,并且元素宽和高都不等于 0

visibilityOfElementLocated( By locator)

11,判断某组元素是否可见

visibilityOfAllElementsLocatedBy( By locator)

12,判断某个元素是否可见. 可见代表元素非隐藏,并且元素宽和高都不等于 0,传入类型为:element

visibilityOfAllElements(final List<WebElement> elements)

13,判断某个元素中的text是否包含了预期的字符串;

textToBePresentInElement( WebElement element, String text)

14,判断某个元素中的 text 是否 包含 了预期的字符串

textToBePresentInElement(By locator, String text)

15,判断某个元素中的 text 是否 包含 了预期的字符串

textToBePresentInElementLocated(final By locator, final String text)

16,判断某个元素中的 value 属性是否包含 了预期的字符串,传入element

textToBePresentInElementValue( WebElement element, String text)

17,判断某个元素中的 value 属性是否包含 了预期的字符串,传入locator

textToBePresentInElementValue(final By locator, final String text)

18,判断该 frame 是否可以 switch进去,如果可以的话,返回 True 并且 switch 进去,否则返回 False

frameToBeAvailableAndSwitchToIt(final String frameLocator)

19,断该 frame 是否可以 switch进去,如果可以的话,返回 True 并且 switch 进去,否则返回 False,传入locator

frameToBeAvailableAndSwitchToIt(final By locator)

20,某个元素中是否不存在于dom树或不可见;

invisibilityOfElementLocated(final By locator)

21,判断带有文本的元素要么不可见,要么文本不存在于元素上

invisibilityOfElementWithText(final By locator, final String text)

22,是否可点击,判断某个元素中是否可见并且是enable的,这样的话才叫clickable;

elementToBeClickable(final By locator)

23,判断某个元素中是否可见并且是enable的,这样的话才叫clickable;

elementToBeClickable(final WebElement element)

24,判断一个元素是否仍在DOM中,传入WebElement对象,可以判断页面是 否刷新了。

stalenessOf(final WebElement element)

25,refreshed(final ExpectedCondition condition)

26,页面元素处于被选中状态

elementToBeSelected(WebElement element)

27,判断某个元素的选中状态是否符合预期,传入element

elementSelectionStateToBe( WebElement element, boolean selected)

28,判断某个元素是否被选中了,一般用在下拉列表;

elementToBeSelected(By locator)

29,判断某个元素的选中状态是否符合预期,传入locator

elementSelectionStateToBe(final By locator, final boolean selected)

30,判断页面上是否存在alert.返回Ture或False

alertIsPresent()
print(EC.alert_is_present()) ## True or Flase

31、

not(final ExpectedCondition<?> condition)

32、

WebElement findElement(By by, WebDriver driver)

33、

List<WebElement> findElements(By by, WebDriver driver)

Leave a Reply