强制等待和隐式等待示例
背景知识 发送回车键
from selenium.webdriver.common.keys import Keys driver.find_element(By.CSS_SELECTOR, loc).send_keys(Keys.ENTER)
隐式等待的意思是 当符合条件就结束等待,不符合持续等待
对比 第一种 强制等待
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from time import sleep def wait(loc): ele=driver.find_element(By.CSS_SELECTOR,loc) ele.send_keys("1") sleep(1) ele.send_keys(Keys.ENTER) if __name__ == '__main__': driver=webdriver.Chrome() driver.get('https://www.baidu.com') loc='input#kw' sleep(3) print("已经打开百度浏览器,等待3秒") wait(loc) sleep(3) print("回车后,等待3秒")
对比 第二种 隐式等待
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from time import sleep def wait(loc): ele=driver.find_element(By.CSS_SELECTOR,loc) ele.send_keys("1") ele.send_keys(Keys.ENTER) if __name__ == '__main__': driver=webdriver.Chrome() driver.get('https://www.baidu.com') loc='input#kw' driver.implicitly_wait(3) print("已经打开百度浏览器,全局情况下 找元素 等待时长最久3秒") wait(loc) sleep(3) print("回车后,等待3秒")