python selenium三种等待方式 作者:马育民 • 2021-07-16 11:33 • 阅读:10503 # time.sleep 使用 python 的休眠函数 ``` import time time.sleep(3) # 强制等待3秒再执行下一步 ``` ### 优点 简单灵活 ### 缺点 无法准确设置等待时间,比如:设置休眠10秒,但网页1秒钟就加载完成;或者设置休眠3秒,但网页需要10秒才加载完成 # 隐性等待 隐形等待是设置了一个最长等待时间,如果在规定时间内网页加载完成,则执行下一步;否则一直等到时间截止,然后执行下一步 ``` from selenium import webdriver browser = webdriver.Chrome() browser.implicitly_wait(30) # 隐性等待,最长等30秒 ``` ### 全局有效 隐性等待对整个driver的周期都起作用,所以只要 **设置 一次 即可** ### 优点 简单 ### 缺点 - 不够灵活,等待的时间是 **全局的**,一旦设置,都按这个等待时间执行。但是不同元素、不同页面、甚至不同网络的加载情况不一样 - 程序会一直等待 **整个页面加载完成**,也就是 **刷新按钮不再转**,才会执行下一步,但有时候页面想要的 **元素 早就在加载完成了** ### 例子 ``` from selenium import webdriver browser = webdriver.Chrome() browser.get('http://8.131.93.32:8081/mgr/login.html') browser.implicitly_wait(30) # 隐性等待,最长等30秒 # 输入框输入内容 browser.find_element_by_id("username").send_keys("admin") browser.find_element_by_id("password").send_keys("123456") browser.find_element_by_xpath("/html/body/div/form/input[3]").click() browser.find_element_by_xpath('//*[@id="nav"]/li/a').click() browser.find_element_by_xpath('//*[@id="nav"]/li/ul/li[1]/a').click() ``` # 显性等待 WebDriverWait,配合该类的 `until()` 和 `until_not()` 方法,就能够根据判断条件而进行灵活地等待了。 过程:程序每隔xx秒看一眼,如果条件成立了,则执行下一步,否则继续等待,直到超过设置的最长时间,然后抛出TimeoutException。 ### WebDriverWait类 wait模块的WebDriverWait类是显性等待类: ``` selenium.webdriver.support.wait.WebDriverWait ``` ##### init(driver,timeout,poll_frequency,ignored_exceptions) - driver: 传入WebDriver实例,即我们上例中的driver - timeout: 超时时间,等待的最长时间(同时要考虑隐性等待时间) - poll_frequency: 调用until或until_not中的方法的间隔时间,默认是0.5秒 - ignored_exceptions: 忽略的异常,如果在调用until或until_not的过程中抛出这个元组中的异常,则不中断代码,继续等待,如果抛出的是这个元组外的异常,则中断代码,抛出异常。默认只有NoSuchElementException。 ##### until(method,message) 方法 - method: 在等待期间,每隔一段时间(__init__中的poll_frequency)调用这个传入的方法,直到返回值不是False - message: 如果超时,抛出TimeoutException,将message传入异常 ##### until_not(method,message) 方法 与until相反,until是当某元素出现或什么条件成立则继续执行, until_not是当某元素消失或什么条件不成立则继续执行,参数也相同 # 案例 ``` #coding=utf-8 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait base_url = "http://www.baidu.com" driver = webdriver.Firefox() driver.implicitly_wait(5) '''隐式等待和显示等待都存在时,超时时间取二者中较大的''' locator = (By.ID,'kw') driver.get(base_url) WebDriverWait(driver,10).until(EC.title_is(u"百度一下,你就知道")) '''判断title,返回布尔值''' WebDriverWait(driver,10).until(EC.title_contains(u"百度一下")) '''判断title,返回布尔值''' WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,'kw'))) '''判断某个元素是否被加到了dom树里,并不代表该元素一定可见,如果定位到就返回WebElement''' WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,'su'))) '''判断某个元素是否被添加到了dom里并且可见,可见代表元素可显示且宽和高都大于0''' WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element(by=By.ID,value='kw'))) '''判断元素是否可见,如果可见就返回这个元素''' WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'.mnav'))) '''判断是否至少有1个元素存在于dom树中,如果定位到就返回列表''' WebDriverWait(driver,10).until(EC.visibility_of_any_elements_located((By.CSS_SELECTOR,'.mnav'))) '''判断是否至少有一个元素在页面中可见,如果定位到就返回列表''' WebDriverWait(driver,10).until(EC.text_to_be_present_in_element((By.XPATH,"//*[@id='u1']/a[8]"),u'设置')) '''判断指定的元素中是否包含了预期的字符串,返回布尔值''' WebDriverWait(driver,10).until(EC.text_to_be_present_in_element_value((By.CSS_SELECTOR,'#su'),u'百度一下')) '''判断指定元素的属性值中是否包含了预期的字符串,返回布尔值''' #WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it(locator)) '''判断该frame是否可以switch进去,如果可以的话,返回True并且switch进去,否则返回False''' #注意这里并没有一个frame可以切换进去 WebDriverWait(driver,10).until(EC.invisibility_of_element_located((By.CSS_SELECTOR,'#swfEveryCookieWrap'))) '''判断某个元素在是否存在于dom或不可见,如果可见返回False,不可见返回这个元素''' #注意#swfEveryCookieWrap在此页面中是一个隐藏的元素 WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='u1']/a[8]"))).click() '''判断某个元素中是否可见并且是enable的,代表可点击''' driver.find_element_by_xpath("//*[@id='wrapper']/div[6]/a[1]").click() #WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='wrapper']/div[6]/a[1]"))).click() #WebDriverWait(driver,10).until(EC.staleness_of(driver.find_element(By.ID,'su'))) '''等待某个元素从dom树中移除''' #这里没有找到合适的例子 WebDriverWait(driver,10).until(EC.element_to_be_selected(driver.find_element(By.XPATH,"//*[@id='nr']/option[1]"))) '''判断某个元素是否被选中了,一般用在下拉列表''' WebDriverWait(driver,10).until(EC.element_selection_state_to_be(driver.find_element(By.XPATH,"//*[@id='nr']/option[1]"),True)) '''判断某个元素的选中状态是否符合预期''' WebDriverWait(driver,10).until(EC.element_located_selection_state_to_be((By.XPATH,"//*[@id='nr']/option[1]"),True)) '''判断某个元素的选中状态是否符合预期''' driver.find_element_by_xpath(".//*[@id='gxszButton']/a[1]").click() instance = WebDriverWait(driver,10).until(EC.alert_is_present()) '''判断页面上是否存在alert,如果有就切换到alert并返回alert的内容''' print instance.text instance.accept() driver.close() ``` 原文出处:http://www.malaoshi.top/show_1IX1V4bn2eMq.html