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