如何保證元素定位的成功率(等待機制)

"""
隱性等待:driver.implicitly_wait(5)
優勢:1) 設置的是最長等待時間,只有在頁面徹底加載完成才執行下一步(徹底加載:左上角的全不在轉了)
    2) 隱性等待對整個driver的週期都起做用,只要設置一次就好了
合理的加載方式:檢測個人元素,是否被加載到
顯性等待: 
總結:隱性等待和顯性等待能夠同時使用,等待時間取決於二者之間的最大者
"""
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


driver = webdriver.Chrome()
driver.implicitly_wait(10)
# 隱性等待與顯示等待能夠同時使用
driver.get("https://www.baidu.com/")
# 參數:最長等待時間,間隔多長時間去查詢一次
WebDriverWait(driver,15,0.5).until(EC.presence_of_all_elements_located((By.LINK_TEXT,'登陸')))
print(driver.find_element_by_link_text('登陸').get_attribute("href"))
相關文章
相關標籤/搜索