# 元素等待
# 1.元素等待是一个针对某一个元素进行相关等待判定
# 2.隐式等待不针对某一个元素进行等待,全局元素等待
# a.相关模块
# WebDriverWait显示等待针对元素比用
# expected_conditions 预期条件类(里面包含方法可以调用,用于显示等待)
# NoSuchElementException 用于隐士等待抛出异常
# By用于元素定位
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 selenium.common.exceptions import NoSuchAttributeException
from time import sleep
#案例:检测百度页面搜索按钮是否存在,存在就输入“好好学习”,然后点击搜索
driver=webdriver.Chrome()
driver.get("https://www.baidu.com/")
driver.find_element_by_css_selector('#kw').send_keys("好好学习")
#显示等待,判断搜索按钮是否存在 (单个元素)
#5代表等待最长5秒,0.5代表每0.5秒检测一次,单个元素为ID='su'
element=WebDriverWait(driver,5,0.5).until(EC.presence_of_element_located((By.ID,"su")))
element.click() #刷新出来后,点击
sleep(3)
driver.quit()