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()