隐形等待
from selenium import webdriver#导包 driver=webdriver.Chrome()#生成一个谷歌浏览器的操作对象 driver.get('https://www.baidu.com/') driver.implicitly_wait(30)#隐性等待,最长30s
显示等待
import time from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium import webdriver#导包 driver=webdriver.Chrome()#生成一个谷歌浏览器的操作对象 driver.get('https://www.baidu.com/') driver.maximize_window() driver.find_element_by_id('kw').send_keys("这是使用id定位,然后键盘输入") driver.find_element_by_class_name('toindex').click() driver.find_element_by_link_text('登录').click() time.sleep(2)
要定位和等待的目标元素
xpath_element='//*[@id="passport-login-pop-dialog"]/div/div/div/div[3]/a'
WebDriverWait(driver,10,1)设置 等待10秒钟,每隔1秒去查看对应的元素是否可见;如果可见,继续下一步操作;如果不可见,则继续等待,直到10s结束,如果元素还是不可见,则抛出超时异常
until(EC.visibility_of_element_located((By.XPATH,xpath_element)))设置等待的的目标元素
WebDriverWait(driver,10,1).until(EC.visibility_of_element_located((By.XPATH,xpath_element)))
代码到这里就说明元素等到了,开始执行点击
driver.find_element_by_xpath('//*[@id="passport-login-pop-dialog"]/div/div/div/div[3]/a').click()#点击注册
强制等待
time.sleep(2) driver.find_element_by_xpath('//*[@id="passport-login-pop-dialog"]/div/div/div/div[3]/a').click()