【轉】selenium webdriver三種等待方法

原文:https://www.cnblogs.com/lgh344902118/p/6015593.htmlhtml

webdriver三種等待方法web

1.使用WebDriverWait數組

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait                            # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC           # available since 2.26.0
driver = 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()

presence_of_element_located能夠替換爲dom

  • 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 - it is Displayed and Enabled.
  • 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

By.ID中的ID可替換爲'CLASS_NAME', 'CSS_SELECTOR', 'ID', 'LINK_TEXT', 'NAME', 'PARTIAL_LINK_TEXT', 'TAG_NAME', 'XPATH'ui

二、隱式等待,至關於設置全局的等待,在定位元素時,對全部元素設置超時時間。url

隱式等待使得WebDriver在查找一個Element或者Element數組時,每隔一段特定的時間就會輪詢一次DOM,若是Element或數組沒有立刻被發現的話。spa

默認設置是0。code

一旦設置,這個隱式等待會在WebDriver對象實例的整個生命週期起做用。htm

from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(10)                      # seconds
driver.get(http://www.xxx.com)
myDynamicElement = driver.find_element_by_id("ElementID")

三、強制等待對象

import time

time.sleep(10)
相關文章
相關標籤/搜索