Selenium2+Python--等待页面元素加载(wait)


在进行web自动化的时候,受页面加载速度影响比较大,常常会报element not found的错误。selenium1.0 中提供了selenium.isElementPresent(Xpath),用于判断xpath是否存在,存在就执行操作,不存在就可以等待一定的时间段。在webDriver中提供了WebDriverWait类,可以智能的等待页面元素加载完成再执行操作。


利用这个类就可以实现智能等待的效果:

     wait = WebDriverWait(webdriver.chrome(),30)

  elm = wait.until(lambda x: x.find_element_by_xpath(Xpath))

  elm.click()

 

再提供一个判断元素存在否:

  def isPresent(self):
  try: driver.find_element_by_xpath(Xpath)
  except NoSuchElementException, e: return False
  return True


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM