Selenium with Python中文翻译(五)

来源:互联网 发布:mac怎么玩魔兽 编辑:程序博客网 时间:2024/05/20 07:15

5.等待
现如今很多web apps运用了AJAX 技术。当浏览器加载一个网页时,网页内的元素会在不同的时间间隔内被加载。这会使定位元素变得困难:我们并不能确切的知道元素是否已经在DOM树里,定位函数将会引发ElementNotVisibleException。运用waits,我们能解决这个问题。等待在执行的动作之间提供了缓冲尤其是当你在执行定位元素或者其它的元素操作时。
Selenium Webdriver提供了两种类型的waits分别是implicit&explicit 。一个显示的等待某个条件发生再进行下一步操作。当定位一个元素时,一个显示等待会使WebDriver在等待一定时间后获得DOM树。
5.1.显示等待
一个显示等待是你定义的在某个条件发生之后再进行下一步操作的代码。一个极端的情况是利用time.sleep(),它会为条件设置一个确切的时间。这里有一些方便的办法帮助你,只为你编写的请求代码来设置等待。WebDriverWait和ExceptedCondition是一种可以实现的组合。

from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECdriver = webdriver.Firefox()driver.get("http://somedomain/url_that_delays_loading")try:    element = WebDriverWait(driver, 10).until(        EC.presence_of_element_located((By.ID, "myDynamicElement"))    )finally:    driver.quit()

如果再10秒之内找到元素那么便不会引发TimeoutException。默认情况下,WebDriver会在每500毫秒调用一次Expeced直到返回成功。ExpectedCondition类返回的成功值是布尔值比如true或者not null对于其它ExpectedCondition类的返回值也是这样。
Expected Conditions
在你自动化浏览网页时有很多常用的常见条件。下面是一个包含了这些常用条件名字的列表。Selenium的Python绑定提供了一些方便的方法,所以直接导入就可以了不用自己编写expected_condition类。

  • title_is
  • title_contains
  • presence_of_element_located
  • visibility_of_element_located
  • visibility_of
  • presence_of_all_elements_located
  • text_to_be_present_in_element
  • text_to_be_present_in_element_value
  • frame_to_be_available_and_switch_to_it
  • invisibility_of_element_located
  • element_to_be_clickable
  • staleness_of
  • element_to_be_selected
  • element_located_to_be_selected
  • element_selection_state_to_be
  • element_located_selection_state_to_be
  • alert_is_present*
from selenium.webdriver.support import expected_conditions as ECwait = WebDriverWait(driver, 10)element = wait.until(EC.element_to_be_clickable((By.ID, 'someid')))

expected_conditions模块提供了一组与WebDriverWait一起使用预定义条件。
定制Wait Conditions
如果没有明确方便的方法符合你的需求,你可以自己定制wait conditations。一个定制等待条件可以用包含__call__方法的类来实现,当匹配不成功时会返回False。

class element_has_css_class(object):  """An expectation for checking that an element has a particular css class.  locator - used to find the element  returns the WebElement once it has the particular css class  """  def __init__(self, locator, css_class):    self.locator = locator    self.css_class = css_class  def __call__(self, driver):    element = driver.find_element(*self.locator)   # Finding the referenced element    if self.css_class in element.get_attribute("class"):        return element    else:        return False# Wait until an element with id='myNewInput' has class 'myCSSClass'wait = WebDriverWait(driver, 10)element = wait.until(element_has_css_class((By.ID, 'myNewInput'), "myCSSClass"))

5.2.隐式等待
对于寻找一些不是能马上获得的元素,一个隐式等待能够让WebDriver在获取DOM树时等待一段时间。默认设置为0。设置后,隐式等待将会为WebDriver对象的寿命来设置等待时间。

from selenium import webdriverdriver = webdriver.Firefox()driver.implicitly_wait(10) # secondsdriver.get("http://somedomain/url_that_delays_loading")myDynamicElement = driver.find_element_by_id("myDynamicElement")

如有错误,敬请指正!

原创粉丝点击