from selenium import webdriver from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.common.by import By driver = webdriver.Chrome() driver.maximize_window() driver.get('http://www.baidu.com/') # 检查title print(EC.title_contains("百度一下,你就知道")) # 判断元素是否存在 baidu_butn_element = driver.find_element_by_xpath("//*[contains(@class,'s_btn_wr')]") EC.presence_of_element_located(baidu_butn_element) # 判断元素可见 EC.visibility_of_element_located(baidu_butn_element) locater = (By.XPATH, "//*[contains(@class,'bg s_btn_wr')]") # 查找元素等待5秒直到这个元素可见 只用传定位方式 WebDriverWait(driver, 5).until(EC.visibility_of_element_located(locater)) # 关闭浏览器 driver.close()