python selenium 等待元素出现_Python+selenium 元素等待

强制等待(time.sleep)

通过time模块的sleep方法让程序睡眠

# coding=utf-8

import time # 导入time模块

time.sleep(second) # 程序休眠,其中second为休眠时间(单位为秒)

隐性等待(implicitly_wait)

设置隐性等待后每次driver执行,页面加载完成后找不到元素都会等待设置的时间;

隐性等待是全局有效的,值设置的过长对用例执行效率有很大的影响,必须在执行完成之后还原回来

# coding=utf-8

driver.implicitly_wait(5) # 设置隐性等待为5秒

driver.implicitly_wait(0) # 还原程序的隐性等待

显示等待(WebDriverWait)

from selenium.webdriver.support.wait import WebDriverWait

WebDriverWait(driver, timeout, poll_frequency, ignored_exceptions)

程序每隔X秒(poll_frequency默认为0.5)看一眼,如果条件成立了,则执行下一步,否则继续等待,直到超过设置的最长时间(timeout),然后抛出TimeoutException

driver: 传入WebDriver实例,即我们上例中的driver

timeout: 超时时间,等待的最长时间(同时要考虑隐性等待时间)

poll_frequency: 调用until或until_not中的方法的间隔时间,默认是0.5秒

ignored_exceptions:忽略的异常,如果在调用until或until_not的过程中抛出这个元组中的异常,则不中断代码,继续等待,如果抛出的是这个元组外的异常,则中断代码,抛出异常; 默认NoSuchElementException

WebDriverWait(driver, timeout, poll_frequency, ignored_exceptions).until(method, message)

until:当某元素出现或什么条件成立则继续执行(until_not:当某元素消失或什么条件不成立则继续执行)

method:在等待期间,每隔一段时间(poll_frequency)调用这个传入的方法,直到返回值不是False

message::如果超时,抛出TimeoutException,将message传入异常

expected_conditions

WebDriverWait()下的unit和unit_not方法能够根据expected_conditions模块提供的方法进行灵活地等待

以百度搜索框为例:

# coding=utf-8

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.by import By

from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome()

driver.get("https://www.baidu.com")

WebDriverWait(driver, 5).until(EC.presence_of_all_elements_located((By.ID, "kw")),message='元素kw未出现')

driver.find_element_by_id('kw').send_keys('selenium')

driver.find_element_by_id('su').click()

driver.quit()

1.判断当前页面的title是否精确等于预期

WebDriverWait(driver,10).until(EC.title_is(u"百度一下,你就知道"))

2.判断当前页面的title是否包含预期字符串

WebDriverWait(driver,10).until(EC.title_contains(u"百度一下"))

3.判断某个元素是否被加到了dom树里,并不代表该元素一定可见,通俗易懂点就是元素存不存在这个页面

WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,'kw')))

4.判断是否至少有1个元素存在于dom树中,如果页面上有n个元素的class都是'column-md-3',那么只要有1个元素存在,这个方法就返回True

WebDriverWait(driver, 5).until(EC.presence_of_all_elements_located((By.ID, "kw")))

5.判断某个元素是否可见.可见代表元素非隐藏,并且元素的宽和高都不等于0

WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.ID, 'k')))

6.判断元素是否可见,如果可见就返回这个元素

WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element(by=By.ID,value='kw')))

7.判断某个元素是否被选中了,一般用在下拉列表

WebDriverWait(driver,10).until(EC.element_to_be_selected(driver.find_element(By.XPATH,"//*[@id='nr']/option[1]")))

8.判断页面上是否存在alert,如果有就切换到alert并返回alert的内容

instance = WebDriverWait(driver,10).until(EC.alert_is_present())

9.判断该frame是否可以switch进去,如果可以的话,返回True并且switch进去,否则返回False

WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it(locator))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值