有时使用隐士等待不起作用,建议使用显示等待,将显示等待封装成一个方法,直接调用
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
def Explicit_Waits(driver,way,path):
try:
ele = WebDriverWait(driver,10).until(
EC.presence_of_element_located((way,path)))
return ele
except Exception as e:
print('元素寻找失败: '+str(e))
#调用此方法
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
#By.ID直接传入,元素path传入字符串
#By.后的方法是全大写如By.XPATH
ele = Explicit_Waits(driver,By.ID,'kw')
ele.send_keys('hello')