Advanced web automation testing: the application and principle of expected_conditions in web pages

foreword

Expected_conditions is a module of selenium, which can judge the elements on the web page, and is generally used with WebDriverWait.

Detailed introduction

1. title_is, to determine whether the title of the current page is equal to the expected value, and return a Boolean value

You can also use driver.title to print the title of the current page

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.set_window_size(480, 600)
driver.get('https://www.baidu.com')

# 判断当前页面的标题是否符合预期
# title = WebDriverWait(driver, 5).until(EC.title_is('百度一下,你就知道'))
# 返回的类型是True或False
# print(title)
if EC.title_is('百度一下,你就知道'):
    print(True)
print(driver.title)
driver.quit()

2. title_contains(), to determine whether the title of the current page contains the expected string

The usage is similar to title_is(), and the introduction will not be repeated

3. presence_of_element_located() determines whether an element is added to the DOM tree, but it does not mean that the element must be visible

Note: The parameter passed in is a tuple type, the first element is the positioning method, and the second element is the value. Returns the element position if the element exists, throws an exception (NoSuchElementException) if it does not exist

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.set_window_size(480, 600)
driver.get('https://www.baidu.com')

r = EC.presence_of_element_located(('id', 'kw'))
print(r(driver))

4. visibility_of_element_located() determines whether the element is visible (visible means that the element is not hidden, and the width and height of the element are not equal to 0)

Compared with presence_of_element_located() type, the difference is that presence_of_element_located() only emphasizes that the element exists in the DOM tree, and it does not matter whether it is visible or not.
Returns the element itself if the element exists and is visible, false if it exists but is not visible

5. visibility_of() determines whether the element is visible

The function is the same as visibility_of_element_located(), the difference is that the parameters are different, and the parameter received by visibility_of() is the positioned element

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.set_window_size(480, 600)
driver.get('https://www.baidu.com')

element = driver.find_element_by_id('kw')
locate = WebDriverWait(driver, 5).until(EC.visibility_of(element))
locate.send_keys('selenium')
driver.quit()

6、presence_of_all_element_located()

Determine whether at least one element exists in the DOM tree

7、text_to_be_present_in_element()

Determine whether the text in an element contains the expected string

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.set_window_size(480, 600)
driver.get('https://www.baidu.com')

r = WebDriverWait(driver, 5).until(EC.text_to_be_present_in_element(('xpath', '//*[@id="bottom_layer"]/div[1]/p[5]/a'), '使用百度前必读'))
print(r)

8、text_to_be_present_in_element_value()

Determine whether the value attribute in an element contains the expected string

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.set_window_size(480, 600)
driver.get('https://www.baidu.com')

# 判断某个元素的value属性值是否符合预期字符串
r = WebDriverWait(driver, 5).until(EC.text_to_be_present_in_element_value(('id', 'su'), '百度一下'))
print(r)

9、frame_to_be_available_and_switch_to_it()

Determine whether the form can be switched in. If yes, return True and switch in, otherwise return False.
For example, log in to QQ mailbox:

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

def find_element(located):
    if isinstance(located, tuple):
        return WebDriverWait(driver, 5).until(EC.presence_of_element_located(located))
    else:
        return '必须是元组类型'

driver = webdriver.Chrome()
driver.set_window_size(600, 800)

driver.get('https://mail.qq.com/')
# 判断iframe是否可以切换进去,返回布尔值
is_switch = WebDriverWait(driver, 5).until(EC.frame_to_be_available_and_switch_to_it(('id', 'login_frame')))
print(is_switch)

# 点击【帐号密码登录】
WebDriverWait(driver, 5).until(EC.presence_of_element_located(('id', 'switcher_plogin'))).click()

# 定位元素:账号输入框、密码输入框、登录按钮
user_element = find_element(('id', 'u'))
password_element = find_element(('id', 'p'))
login_element = find_element(('id', 'login_button'))

# 输入账号、密码、点击登录
user_element.send_keys('[email protected]')
password_element.send_keys('password')
login_element.click()

driver.quit()

10、invisibility_of_element_located()

Determine whether an element does not exist in the DOM tree or is not visible

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.set_window_size(480, 600)
driver.get('https://www.baidu.com')

a = EC.invisibility_of_element_located(('id', 'kw')) # 存在则返回False
print(a(driver))

11、element_to_be_clickable()

Determine whether the element is visible and clickable
If the element exists and is clickable, return the element, if the element exists but is not clickable, return False

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.set_window_size(480, 600)
driver.get('https://www.baidu.com')

# 百度页该元素不可点击,返回False
a = EC.element_to_be_clickable(('xpath', '//*[@id="s-bottom-layer-right"]/span[2]'))
print(a(driver))

12、staleness_of()

Wait until an element is removed from the DOM tree and pass in the element element to determine whether the page has been refreshed

13、element_to_be_selected()

Determine whether an element is selected, generally used in the drop-down list, pass in the element element

14、element_selection_state_to_be()

Determine whether the selected state of an element meets expectations, the method parameters are element and is_selected

15、element_located_selection_state_to_be()

It has the same function as element_selection_state_to_be(), but the method parameters are different, and the method parameters are located and is_selected

16、alert_is_present()

Press whether there is an alert warning on the page

Finally: In order to give back to the die-hard fans, I have compiled a complete software testing video learning tutorial for you. If you need it, you can get it for free【保证100%免费】

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.

Guess you like

Origin blog.csdn.net/IT_LanTian/article/details/131537807