python3自動化實踐15之時間等待總結

implicitly_wait()隱式等待,設置頁面等待加載的最長時間,這段時間不管頁面需要操作的元素是否加載出來,都需要等待指定時間

WebDriverWait(),設置的時間內,默認每隔一段時間檢查下元素是否加載出來,如果加載處理就立即執行下面的元素操作

因此隱式和顯示時間等待結合起來用,不會造成等待時間浪費

from selenium import webdriver

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC

from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Firefox()

driver.implicitly_wait(10)

driver.get("https://www.baidu.com")

locator = (By.ID,"kw")

try:

    WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located(locator))

    driver.find_element_by_id("kw").send_keys("selenium")

except NoSuchElementException as e:

    print(e)

finally:

    driver.quit()


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章