Selenium显性等待
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ECS
from selenium.webdriver.common.by import By
import time
browser = webdriver.Chrome(executable_path='/Users/xx/python/chromedriver')
browser.get('https://www.baidu.com/')
loc = (By.LINK_TEXT, "关于百度")
try:
WebDriverWait(driver, 5).until(ECS.presence_of_element_located(loc))
except:
print("没有找到对应元素!")
finally:
driver.find_element_by_link_text('关于百度').click()
browser.quit(
知道显性等待怎么用了,接下来我们来看下,还有哪些其他常见的使用方式:
先来个简单的,这里通过页面标题来判断
WebDriverWait(driver,3).until(ECS.presence_of_element_located((By.ID,'kw')))
WebDriverWait(driver,3).until(ECS.visibility_of_element_located((By.ID,'su')))
WebDriverWait(driver,3).until(ECS.visibility_of((By.ID,value='su')))
WebDriverWait(driver,3).until(ECS.presence_of_all_elements_located((By.CSS_SELECTOR,'.uinv')))
WebDriverWait(driver,3).until(ECS.visibility_of_any_elements_located((By.CSS_SELECTOR,'.uinv')))
WebDriverWait(driver,3).until(ECS.invisibility_of_element_located((By.CSS_SELECTOR,'#nwWrap')))```
在平时使用中,也经常会用到等待的元素包含某些内容来判断,而且这种方式能适用很多场景
在平时使用中,也经常会用到等待的元素包含某些内容来判断,而且这种方式能适用很多场景
WebDriverWait(driver,3).until(ECS.text_to_be_present_in_element((By.XPATH,"//*[@id='bottom_layer']/div/p[1]/a"),'关于百度'))
WebDriverWait(driver,3).until(ECS.text_to_be_present_in_element_value((By.CSS_SELECTOR,'#su'),'百度一下'))
下面几种方式主要是针对下拉框类型的等待,还有可点击的组件,如超链接
WebDriverWait(driver,3).until(ECS.element_to_be_clickable((By.XPATH,"//*[@id='bottom_layer']/div/p[1]/a"))).click()
WebDriverWait(driver,3).until(ECS.element_to_be_selected((By.XPATH,"//*[@id='sel']/option[2]")))
WebDriverWait(driver,3).until(ECS.element_selection_state_to_be((By.XPATH,"//*[@id='sel']/option[1]"),False))
以下是比较特殊的几种方式,在某些情况下用处很大
WebDriverWait(driver,3).until(ECS.alert_is_present())
WebDriverWait(driver,3).until(ECS.staleness_of(driver.find_element(By.ID,'su')))
WebDriverWait(driver,3).until(ECS.frame_to_be_available_and_switch_to_it(frame1))
实例
import random
from appium import webdriver
'''
导入显性等待模块
'''
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
'''
#页面渲染,显性等待WebDriverWait(driver,超时的时间)。until:等待它达成某一个事件
WebDriverWait(driver,10).until(EC.presence_of_element_located()) #等待一个元素存在于界面代码中,资源加载好了,还没渲染的情况下
WebDriverWait(driver,10).until(EC.visibility_of_element_located()) #等待一个元素显示于界面中,资源加载好了,也渲染出了它的样子(displayed=ture),对其他属性不一定渲染出来了
WebDriverWait(driver,10).until(EC.invisibility_of_element_located()) ##等待一个元素显示于界面中,资源加载好了,没有渲染出了它的样子(displayed=flase),对其他属性不一定渲染出来了
WebDriverWait(driver,10).until(EC.element_to_be_clickable()) # #等待一个元素显示于界面中,资源加载好了,也渲染出了它的样子(displayed=ture),对其他属性(clickable==true)一定渲染出来了,一定可以被点击的元素
WebDriverWait(driver,10).until(EC.element_to_be_selected())#等待一个元素显示于界面中,资源加载好了,也渲染出了它的样子(displayed=ture),对其他属性(selected=ture)一定渲染出来了,一定可以被选择的元素
'''
"""
脚本目的:
验证通过搜索功能的书籍,与从书城中获取的书籍一致
"""
'''
步骤:
a、打开读书屋,进入书城(需要构建一个会话)
'''
desired_caps={
"platformName":"android",
"platformVersion":"7.1.2",
"deviceName":"emulator-5554",
"app":r"C:\Users\admin\Desktop\1\dushuwu.apk" ,
"noReset":False
}
driver=webdriver.Remote(command_executor="http://127.0.0.1:4723/wd/hub",desired_capabilities=desired_caps)
driver.implicitly_wait(10)
locator=(MobileBy.ACCESSIBILITY_ID,"书城")
WebDriverWait(driver,10).until(EC.presence_of_element_located(locator=locator)).click()
locator=(MobileBy.XPATH,'//*[@resource-id="com.zhao.myreader:id/rv_book_list"]/android.widget.LinearLayout')
books=WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located(locator))
'''
# b、随机获取一本书为验证点数据
'''
suiji=random.randint(0,len(books)-1)
book=books[suiji]
locator = (MobileBy.ID, "com.zhao.myreader:id/tv_book_name");
shuming=WebDriverWait(book,10).until(EC.visibility_of_element_located(locator)).text
locator=(MobileBy.ID,"com.zhao.myreader:id/tv_book_desc")
miaoshu=WebDriverWait(book,10).until(EC.visibility_of_element_located(locator)).text
locator=(MobileBy.ID,"com.zhao.myreader:id/tv_book_author")
zuozhe=WebDriverWait(book,10).until(EC.visibility_of_element_located(locator)).text
book.click()
locator=(MobileBy.ID,"com.zhao.myreader:id/btn_read_book")
WebDriverWait(driver,10).until(EC.element_to_be_clickable(locator)).click()
locator=(MobileBy.ID,"com.zhao.myreader:id/tv_content")
WebDriverWait(driver,10).until(EC.element_to_be_clickable(locator)).click()
locator=(MobileBy.ID,"com.zhao.myreader:id/ll_chapter_list")
WebDriverWait(driver,10).until(EC.element_to_be_clickable(locator)).click()
locator=(MobileBy.XPATH,'//*[@resource-id="com.zhao.myreader:id/lv_chapter_list"]/android.widget.LinearLayout')
zhangjie=WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located(locator))
cuncu=dict()
for index,element in enumerate(zhangjie):
locator=(MobileBy.ID,"com.zhao.myreader:id/tv_chapter_title")
cuncu.update({index:WebDriverWait(element,10).until(EC.visibility_of_element_located(locator)).text})
'''
# d、依照书名去搜索,返回到搜索书城去搜索需要返回2次
'''
driver.back()
driver.back()
locator=(MobileBy.ID,"com.zhao.myreader:id/iv_search")
WebDriverWait(driver,10).until(EC.visibility_of_element_located(locator)).click()
locator=(MobileBy.ID,"com.zhao.myreader:id/et_search_key")
WebDriverWait(driver,10).until(EC.visibility_of_element_located(locator)).send_keys(shuming)
locator=(MobileBy.ID,"com.zhao.myreader:id/tv_search_conform")
WebDriverWait(driver,10).until(EC.visibility_of_element_located(locator)).click()
locator=(MobileBy.XPATH,'//*[@resource-id="com.zhao.myreader:id/lv_search_books_list"]/android.widget.LinearLayout')
search_books=WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located(locator))
search_index=None
for i in search_books:
locator=(MobileBy.ID,"com.zhao.myreader:id/tv_book_name")
bn=WebDriverWait(driver,10).until(EC.visibility_of_element_located(locator)).text
if shuming==bn:
search_index=search_books.index(i)
if search_index is None:
raise ValueError(f"没有找到:{shuming}")
else:
search_book=search_books[search_index]
locator=(MobileBy.ID,"com.zhao.myreader:id/tv_book_name")
search_shuming=WebDriverWait(driver,10).until(EC.visibility_of_element_located(locator)).text
locator=(MobileBy.ID,"com.zhao.myreader:id/tv_book_desc")
search_miaoshu=WebDriverWait(driver,10).until(EC.visibility_of_element_located(locator)).text
locator=(MobileBy.ID,"com.zhao.myreader:id/tv_book_author")
search_zuozhe=WebDriverWait(driver,10).until(EC.visibility_of_element_located(locator)).text
search_book.click()
locator=(MobileBy.ID,"com.zhao.myreader:id/btn_read_book")
WebDriverWait(driver,10).until(EC.visibility_of_element_located(locator)).click()
locator=(MobileBy.ID,"com.zhao.myreader:id/tv_content")
WebDriverWait(driver,10).until(EC.visibility_of_element_located(locator)).click()
locator=(MobileBy.ID,"com.zhao.myreader:id/ll_chapter_list")
WebDriverWait(driver,10).until(EC.visibility_of_element_located(locator)).click()
locator=(MobileBy.XPATH,'//*[@resource-id="com.zhao.myreader:id/lv_chapter_list"]/android.widget.LinearLayout')
search_zhangjie=WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located(locator))
search_cuncu=dict()
for search_index,search_element in enumerate(search_zhangjie):
locator=(MobileBy.ID,"com.zhao.myreader:id/tv_chapter_title")
search_cuncu.update(
{search_index:WebDriverWait(search_element,10).until(EC.visibility_of_element_located(locator)).text})
if shuming==search_shuming and miaoshu==search_miaoshu and zuozhe==search_zuozhe and cuncu==search_cuncu:
print("通过搜索功能搜索出的书籍与书城中获取的书籍是相同的")
else:
print("验证失败")
print(f"shuming:{shuming}")
print(f"search_shuming:{search_shuming}")
print(f"zuozhe:{zuozhe}")
print(f"search_zuozhe:{search_zuozhe}")
print(f"miaoshu:{miaoshu}")
print(f"search_miaoshu:{search_miaoshu}")
print(f"zhangjie:{zhangjie}")
print(f"search_zhangjie:{search_zhangjie}")
driver.quit()
强制等待-time.sleep(2)
点击是地方加上等待时间即可
driver.find_element_by_accessibility_id("书城").click()
time.sleep(2)
命令等待
newCommandTimeout
封装等待方式
"""
验证通过搜索功能搜索的书籍,与从书城中获取的书籍是相同的;
首先,我们要完成 从书城中获取数据,并获取需要验证的数据;验证的数据不需要太全,但是也不能太少;
比如,验证数据,我们可以选择为:
1、书籍名称
2、数据的作者信息
3、数据的段落信息
4、数据的描述信息
"""
import random
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from appium.webdriver.common.mobileby import MobileBy
from appium import webdriver
desired_capability = {
"platformName": "android",
"platformVersion": '10.0.0',
"deviceName": 'CUYDU19625011655',
"app": "/Volumes/huace/APP自动化测试课程/class04/课程脚本/pythonProject/dushuwu.apk",
"noReset": True,
}
driver = webdriver.Remote(command_executor="http://localhost:4723/wd/hub", desired_capabilities=desired_capability)
def wait_ele_clickable(driver, by, value):
locator = (by, value)
return WebDriverWait(driver, 10).until(EC.element_to_be_clickable(locator))
def wait_eles_visibility(driver, by, value):
locator = (by, value)
return WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located(locator))
def wait_ele_visibility(driver, by, value):
locator = (by, value)
return WebDriverWait(driver, 10).until(EC.visibility_of_element_located(locator))
wait_ele_clickable(driver, MobileBy.ACCESSIBILITY_ID, "书城").click()
book_elements = wait_eles_visibility(driver, MobileBy.XPATH,
'//*[@resource-id="com.zhao.myreader:id/rv_book_list"]/android.widget.LinearLayout')
rand_index = random.randint(0, len(book_elements) - 1)
book_element = book_elements[rand_index]
book_name = wait_ele_visibility(book_element, MobileBy.ID, "com.zhao.myreader:id/tv_book_name").text
book_desc = wait_ele_visibility(book_element, MobileBy.ID, "com.zhao.myreader:id/tv_book_desc").text
book_author = wait_ele_visibility(book_element, MobileBy.ID, "com.zhao.myreader:id/tv_book_author").text
book_element.click()
wait_ele_clickable(driver, MobileBy.ID, "com.zhao.myreader:id/btn_read_book").click()
wait_ele_clickable(driver, MobileBy.ID, "com.zhao.myreader:id/tv_content").click()
wait_ele_clickable(driver, MobileBy.ID, "com.zhao.myreader:id/ll_chapter_list").click()
chapter_elements = wait_eles_visibility(driver, MobileBy.XPATH,
'//*[@resource-id="com.zhao.myreader:id/lv_chapter_list"]/android.widget.LinearLayout')
book_chapters = dict()
for index, value in enumerate(chapter_elements):
book_chapters.update({index: wait_ele_visibility(value, MobileBy.ID, 'com.zhao.myreader:id/tv_chapter_title').text})
driver.back()
driver.back()
wait_ele_clickable(driver, MobileBy.ID, 'com.zhao.myreader:id/iv_search').click()
locator = (MobileBy.ID, 'com.zhao.myreader:id/et_search_key')
wait_ele_visibility(driver, *locator).send_keys(book_name)
locator = (MobileBy.ID, 'com.zhao.myreader:id/tv_search_conform')
wait_ele_clickable(driver, *locator).click()
locator = (MobileBy.XPATH, '//*[@resource-id="com.zhao.myreader:id/lv_search_books_list"]/android.widget.LinearLayout')
search_books = wait_eles_visibility(driver, *locator)
search_index = None
for i in search_books:
locator = (MobileBy.ID, "com.zhao.myreader:id/tv_book_name")
bn = wait_ele_visibility(i, *locator).text
if book_name == bn:
search_index = search_books.index(i)
if search_index is None:
raise ValueError(f"没有找到: {book_name} 这本书")
else:
search_book = search_books[search_index]
locator = (MobileBy.ID, "com.zhao.myreader:id/tv_book_name")
search_book_name = wait_ele_visibility(search_book, *locator).text
locator = (MobileBy.ID, "com.zhao.myreader:id/tv_book_desc")
search_book_desc = wait_ele_visibility(search_book, *locator).text
locator = (MobileBy.ID, "com.zhao.myreader:id/tv_book_author")
search_book_author = wait_ele_visibility(search_book, *locator).text
search_book_chapters = dict()
search_book.click()
locator = (MobileBy.ID, "com.zhao.myreader:id/btn_read_book")
wait_ele_clickable(driver, *locator).click()
locator = (MobileBy.ID, "com.zhao.myreader:id/tv_content")
wait_ele_clickable(driver, *locator).click()
locator = (MobileBy.ID, "com.zhao.myreader:id/ll_chapter_list")
wait_ele_clickable(driver, *locator).click()
locator = (MobileBy.XPATH, '//*[@resource-id="com.zhao.myreader:id/lv_chapter_list"]/android.widget.LinearLayout')
search_chapter_elements = wait_eles_visibility(driver, *locator)
for index, value in enumerate(search_chapter_elements):
locator = (MobileBy.ID, "com.zhao.myreader:id/tv_chapter_title")
search_book_chapters.update(
{index: wait_ele_visibility(value, *locator).text})
if book_name == search_book_name and book_desc == search_book_desc and book_author == search_book_author and book_chapters == search_book_chapters:
print("通过搜索功能搜索的书籍,与从书城中获取的书籍是相同的")
else:
print("验证失败")
print(f"book_name:{book_name}")
print(f"search_book_name:{search_book_name}")
print(f"book_desc:{book_desc}")
print(f"search_book_desc:{search_book_desc}")
print(f"book_author:{book_author}")
print(f"search_book_author:{search_book_author}")
print(f"book_chapters:{book_chapters}")
print(f"search_book_chapters:{search_book_chapters}")
driver.quit()
|