WebDriverWait显示等待及隐式等待
selenium学习笔记:
WebDriverWait:显式等待,针对某个元素进行等待,结合expected_conditions 使用,expected_conditions下挂条件众多,主要有以下几种:
EC.frame_to_be_available_and_switch_to_it(locator) #判断frame是否可以切换到当前定位地点
EC.text_to_be_present_in_element_value() #判断指定元素的属性值中是否包含了预期的字符串,返回布尔值
import time
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
driver= webdriver.Firefox()
driver.get("https://www.baidu.com/")
locator=(By.ID,"kw")
#判断是否找到element,并返回元素
element = WebDriverWait(driver,5,1).until(EC.presence_of_element_located((By.ID,"kw")))
element.send_keys("selenium")
#判断title是否是设定值,返回布尔值
re= WebDriverWait(driver,10).until(EC.title_is("百度一下,你就知道"))
print(re)
# 判断元素是否可见,返回元素
re1=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,'su')))
print(re1)
time.sleep(4)
driver.quit()
implicitly_wait:隐式等待
#encoding:utf-8 from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait driver=webdriver.Firefox() driver.get("https://www.baidu.com/") driver.implicitly_wait(10)#隐式等待 input_=driver.find_element_by_id("kwss") input_.send_keys("selenium") driver.quit()