分别是显式等待,隐式等待,强制等待
可能会用到的库有:
from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By
三种运行方式的写法如下:
1 """装饰器用来计算函数运行时长""" 2 def decorator_counttime(function): 3 def wrapper(*args, **kargs): 4 starttime = time.time() 5 function(*args, **kargs) 6 endtime = time.time() 7 totaltime = endtime - starttime 8 print( 9 "----------------- the function running use time : %s------------------" 10 % totaltime) 11 12 return wrapper 13 14 15 @decorator_counttime 16 def get_info_from_notification_Forced_wait(): 17 driver = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desires_caps1) 18 time.sleep(10) 19 get_wifi_icon = driver.find_element_by_id( 20 "com.cidana.mirror.asukalink:id/connectButton") 21 print(get_wifi_icon.text) 22 time.sleep(10) 23 get_version = driver.find_element_by_id( 24 "com.cidana.mirror.asukalink:id/textVer") 25 print(get_version.text) 26 driver.close_app() 27 28 29 @decorator_counttime 30 def get_info_from_notification_Explicit_wait(): 31 driver = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desires_caps1) 32 get_wifi_icon = WebDriverWait(driver, 10, 0.5).until( 33 EC.presence_of_element_located( 34 (By.ID, "com.cidana.mirror.asukalink:id/connectButton"))) 35 print(get_wifi_icon.text) 36 get_version = WebDriverWait(driver, 10, 0.5).until( 37 EC.presence_of_element_located( 38 (By.ID, "com.cidana.mirror.asukalink:id/textVer"))) 39 print(get_version.text) 40 driver.close_app() 41 42 43 @decorator_counttime 44 def get_info_from_notification_Implicit_wait(): 45 driver = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desires_caps1) 46 driver.implicitly_wait(10) 47 get_wifi_icon = driver.find_element_by_id( 48 "com.cidana.mirror.asukalink:id/connectButton") 49 print(get_wifi_icon.text) 50 get_version = driver.find_element_by_id( 51 "com.cidana.mirror.asukalink:id/textVer") 52 print(get_version.text) 53 driver.close_app() 54 55 56 get_info_from_notification_Forced_wait() 57 time.sleep(3) 58 get_info_from_notification_Explicit_wait() 59 time.sleep(3) 60 get_info_from_notification_Implicit_wait()
运行结果分别为
显式等待:7.64s
隐式等待:7.84s
强制等待:29.79s