Selenium3自动化测试【26】显式等待

“ 系统讲解Python3+Selenium3如何解决元素加载缓慢的情况”

显示等待

显示等待

显性等待(WebDriverWait),WebDriverWait配合该类的until()和until_not()方法,能够根据判断条件而进行灵活地等待了。它主要的意思是:程序每隔多少秒检查一次,如果条件成立了,则执行下一步,否则继续等待,直到超过设置的最长时间,然后抛出TimeoutException。

WebDriverWait等待也是我们推荐的方法。在使用前我们需要导入WebDriverWait。使用WebDriverWait常常会结合expected_conditions模块一起使用。

from seleniumimport webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("http://cn.bing.com/")

# driver.find_element_by_xpath("//input[@id='sb_form_q']")
element = WebDriverWait(driver,5,0.5).until(EC.presence_of_element_located((By.ID,"sb_form_q")))
element.send_keys("bella")

driver.quit()

WebDriverWait类是WebDriver提供的等待方法,具体格式如下:

WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)

解释如下:

  • driver: 传入WebDriver实例,即代码中的driver;
  • timeout: 超时时间,等待的最长时间(同时要考虑隐性等待时间)
  • poll_frequency: 调用until或until_not中的方法的间隔时间,默认是0.5秒
  • ignored_exceptions: 忽略的异常,如果在调用until或until_not的过程中抛出这个元组中的异常,则不中断代码,继续等待,如果抛出的是这个元组外的异常,则中断代码,抛出异常。默认只有NoSuchElementException。

until

  • method: 在等待期间,每隔一段时间调用这个传入的方法,直到返回值不是False;
  • message: 如果超时,抛出TimeoutException,将message传入异常;
  • until_not 与until相反,until是当某元素出现或什么条件成立则继续执行,until_not是当某元素消失或什么条件不成立则继续执行,两者参数也相同。

使用WebDriverWait常常会结合expected_conditions模块一起使用。
expected_conditions是Selenium的一个模块,其中包含一系列可用于判断的条件。它与10多个condition,与until、until_not组合能够实现很多判断,如果能自己灵活封装,将会大大提高脚本的稳定性。

  • title_is:判断当前页面的title是否完全等于预期字符串,返回是布尔值;
  • title_contains 判断当前页面的title是否包含预期字符串,返回布尔值;
  • presence_of_element_located:判断某个元素是否被加到了dom树里,并不代表该元素一定可见;
  • visibility_of_element_located : 判断某个元素是否可见. 可见代表元素非隐藏,并且元素的宽和高都不等于0;
  • visibility_of :跟上面的方法做一样的事情,只是上面的方法要传入locator,这个方法直接传定位到的element就好了;
  • presence_of_all_elements_located : 判断是否至少有1个元素存在于dom树中。例如,如果页面上有n个元素的class都是'b_searchbox',那么只要有1个元素存在,这个方法就返回True;
  • text_to_be_present_in_element : 判断某个元素中的text是否包含预期的字符串;
  • text_to_be_present_in_element_value:判断某个元素中的value属性是否包含了预期的字符串;
  • frame_to_be_available_and_switch_to_it : 判断该frame是否可以切换(switch)进去,如果可以的话,返回True并且切换进去,否则返回False;
  • invisibility_of_element_located : 判断某个元素中是否不存在于dom树或不可见;
  • element_to_be_clickable : 判断某个元素中是否可见并且是可以单击的,这样的才叫clickable;
  • staleness_of :等某个元素从dom树中移除,返回True或False;
  • element_to_be_selected:判断某个元素是否被选中了,一般用在下拉列表;
  • element_selection_state_to_be:判断某个元素的选中状态是否符合预期;
  • element_located_selection_state_to_be:跟上面的方法作用一样,只是上面的方法传入定位到的element,而这个方法传入locator;
  • alert_is_present : 判断页面上是否存在alert。

隐性等待和显性等待是可以结合起来使用的,案例代码如下:


fromselenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()

driver.implicitly_wait(20) #隐性(式)等待

driver.get("http://cn.bing.com/")
locator = (By.NAME,"q")

try:
    # 显性等待
    WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located(locator))
   driver.find_element_by_xpath("//input[@name='q']").send_keys("bella")
finally:
    driver.quit()

这个案例代码中我们设置了隐性等待和显性等待,在其他操作中,隐性等待起决定性作用,在WebDriverWait中显性等待起主要作用,需要注意的是:最长的等待时间取决于两者之间的大者,此例中为20,隐性等待时间大于显性等待时间,则该句代码的最长等待时间等于隐性等待设置的时间。

=============================================
如果你觉的文章读的不过瘾,可以查看详细的视频教程。
【2021】UI自动化测试:Selenium3自动化测试
https://ke.qq.com/course/3172187?tuin=9c43f38e

【测试全系列视频课程】请点击我哦.....

《全栈测试系列视频》课程

地址:https://ke.qq.com/course/2525707?tuin=9c43f38e

图书京东、当当有售

京东:https://item.jd.com/12784287.html
当当:http://product.dangdang.com/29177828.html)!

posted @ 2022-04-07 22:15  BlaLeo  阅读(181)  评论(0编辑  收藏  举报