Python Selenium如何等待Google People Also Ask加载图标与答案显示
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 from selenium.common.exceptions import TimeoutException driver_path = "你的chromedriver本地路径" driver = webdriver.Chrome(driver_path) driver.maximize_window() # 建议删除隐式等待配置,避免和显式等待混用导致不可预期的等待时长计算错误 # driver.implicitly_wait(30) wait = WebDriverWait(driver, 30) driver.get("https://www.google.com/search?q=How%20to%20make%20bakery%3F&source=hp&ei=j0aZYYjRAvja2roPrcWcyAU&iflsig=ALs-wAMAAAAAYZlUn4NMUPjfIpQmrXSmjIDnaWjJXWIJ&ved=0ahUKEwjI1JDn0Kf0AhV4rVYBHa0iB1kQ4dUDCAc&uact=5&oq=How%20to%20make%20bakery%3F&gs_lcp=Cgdnd3Mtd2l6EAMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBNQAFgAYJMDaABwAHgAgAF-iAF-kgEDMC4xmAEAoAECoAEB&sclient=gws-wiz") # 先等待People Also Ask区域加载完成再获取问题列表 wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='People also ask']"))) all_questions = driver.find_elements(By.XPATH, "//span[text()='People also ask']/../following-sibling::div/descendant::div[@data-hveid and @class and @jsname and @data-ved]") print(len(all_questions)) j = 1 for question in all_questions: # 等待当前问题元素可点击再执行点击操作 ele = wait.until(EC.element_to_be_clickable((By.XPATH, f"(//span[text()='People also ask']/../following-sibling::div/descendant::div[@data-hveid and @class and @jsname and @data-ved])[{j}]"))) j = j + 2 ele.click() # 问题1解决:等待当前点击问题对应的答案区域可见 answer_locator = (By.XPATH, "./../following-sibling::div") try: # 相对于当前点击的问题元素定位答案,避免全局匹配到其他问题的内容 answer_ele = wait.until(EC.visibility_of(ele.find_element(*answer_locator))) except TimeoutException: continue # 问题2解决:等待加载图标完全消失 loading_locator = (By.TAG_NAME, "g-loading-icon") try: # 短时间判断加载图标是否出现,没出现直接跳过等待 WebDriverWait(driver, 2).until(EC.presence_of_element_located(loading_locator)) # 图标出现则等待其完全不可见后再继续 wait.until(EC.invisibility_of_element_located(loading_locator)) except TimeoutException: pass answer = answer_ele.get_attribute('innerText') print(answer) print('--------------') driver.quit()
核心优化逻辑说明
- 答案等待逻辑:放弃随时可能变动的固定class名称匹配,使用相对当前点击元素的路径定位对应答案容器,通过
visibility_of判定答案已经渲染可见,保证取到的内容是完整加载后的结果,不会出现空内容或者加载不全的问题。 - 加载图标等待逻辑:抛弃极易失效的绝对路径XPath,直接匹配Google搜索结果页通用的加载图标标签
g-loading-icon,先短时间判断图标是否出现,出现就等待其消失后再执行后续操作,网络状况好时不会额外增加等待时间。
内容的提问来源于stack exchange,提问作者nguyenphanhoaiduc
相关产品推荐
相关产品推荐

