Selenium-Webdriver(python)学习笔记(七)

----WebdriverWait类(同步点)

WebdriverWait类共有两个可调用方法:until, until_not

WebdriverWait类__init__和until方法(until_not与之类似)的源码

def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):

          (略)

def until(self, method, message=''):
        """Calls the method provided with the driver as an argument until the \
        return value is not False."""
        end_time = time.time() + self._timeout
        while(True):
            try:
                value = method(self._driver)
                if value:
                    return value
            except self._ignored_exceptions:
                pass
            time.sleep(self._poll)
            if(time.time() > end_time):
                break
        raise TimeoutException(message)

实例化:

wait = WebdriverWait(dr, 10) # 只指定超时时间

wait =WebdriverWait(dr, 10, 1) # 指定检查频率

wait =WebdriverWait(dr, 10, ,1, ValueError) # 指定忽略ValueError异常

调用:

调用method时将self._driver作为第一个参数传入.

所以method需要有一个参数

一般传入一个lambda表达式作为判断方式

代码如下:

import os
from distutils import log

from selenium import webdriver
import selenium.webdriver.support.ui as ui

dr = webdriver.Firefox()

dr.get('file:///%s' % (os.path.abspath('wait.html')) )

# 通过class_name定位到SignIn按钮
signIn = dr.find_element_by_class_name('button')
# 输出SignIn按钮的值
log.warn(signIn.get_attribute('value'))

wait = ui.WebDriverWait(dr, 10, 0.001)
signIn.click()

def waitIt(dr):
	theValue = dr.find_element_by_id('theP').text
	log.warn(theValue)
	return theValue == 'Clicked'

log.warn('wait start')
wait.until(waitIt)
# wait.until(lambda dr:dr.find_element_by_id('theP').text == 'Clicked' )

log.warn('wait end')

dr.close()
posted @ 2013-11-06 21:20  _漏斗  阅读(852)  评论(0编辑  收藏  举报