第九章 三种等待方法

1.sleep:进程休眠
— 傻傻的等,会浪费时间(import time)
2. implicitly_wait(30):等待页面完全加载完成
–页面加载完成的标志是左上角转圈结束
–如果页面元素加载完成了,某些js加载失败,页面左上角一直转圈,会耗费时间
– 全局的,只写一次就行了
– 缺点:页面有跳转的时候,它不知道去等跳转后的页面
3. WebDriverWait(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None)
–driver:打开浏览器的一个实例参数,这个不用多说
–timeout:超时的总时长30s
–poll_frequency:循环去查询的间隙时间,默认0.5秒
–ignored_exceptions:忽略异常,默认忽略NoSuchElementException

#coding=utf-8
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import *
driver=webdriver.Firefox()
driver.get("https://www.baidu.com")
#等待时长10秒,默认0.5秒询问一次
element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_xpath(".//*[@id='kw']"))
element.send_keys("yoyo")
#判断页面上不存在某个元素
is_disappeared=WebDriverWait(driver,10,1,(ElementNotVisibleException)).until_not(lambda x:x.find_element_by_xpath(".//*[@id='kw11']").is_displayed())
print(is_disappeared)

猜你喜欢

转载自blog.csdn.net/yxx_bk/article/details/79976855