Selenium 入門到精通系列python
PS:顯式等待、隱式等待、強制等待方法web
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2019-04-23 16:12:33
# @Author : BenLam
# @Link : https://www.cnblogs.com/BenLam/
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
driver=webdriver.Firefox()
driver.get("http://www.baidu.com")
wait=WebDriverWait(driver,5,0.5).until(EC.presence_of_element_located((By.ID,'kw')))
wait.send_keys("TEST")
driver.find_element_by_id("su").click()
#隱式等待
from selenium.common.exceptions import NoSuchElementException
driver.implicitly_wait(10)
driver.get("http://www.baidu.com")
try:
driver.find_element_by_id("kwsssss")
except NoSuchElementException as e:
print(e)
finally:
driver.quit()
#強制等待
driver.get("http://www.baidu.com")
sleep(5)
driver.find_element_by_id("kwsssss")
driver.quit()