You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Selenium获取谷歌图片a标签href返回None的问题排查

问题

我想获取谷歌图片搜索结果中第一张图片对应的<a>标签的href属性(而非<img>标签的src属性),当前代码能定位到Selenium元素,但href属性返回None。

原代码

from selenium import webdriver
from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

chrome_options = webdriver.ChromeOptions()

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.google.com/search?q=5285000201168&sca_esv=560946880&tbm=isch&sxsrf=AB5stBi24FB3-HvM3lZXc7ICrUqeHo3rAQ%3A1693299850492&source=hp&biw=1920&bih=995&ei=irTtZIeVG-PHkdUP96OoiAM&iflsig=AD69kcEAAAAAZO3CmjYgqhpcqRhnPykrz3kO7zPj6_kn&ved=0ahUKEwiHgtbAwYGBAxXjY6QEHfcRCjEQ4dUDCAc&uact=5&oq=5285000201168&gs_lp=EgNpbWciDTUyODUwMDAyMDExNjgyBBAjGCdIwQdQ3wRY3wRwAXgAkAEAmAHBAaABwQGqAQMwLjG4AQPIAQD4AQL4AQGKAgtnd3Mtd2l6LWltZ6gCCsICBxAjGOoCGCc&sclient=img")

search_feild = driver.find_element(By.ID,'REsRA')
search_feild.clear()
search_feild.send_keys("5285000206613")
search_feild.send_keys(Keys.ENTER)

try:
    # Locate the <a> tag element with the specified class attribute using XPath
    link_element = driver.find_element(By.XPATH,"//a[contains(@class, 'wXeWr') and contains(@class, 'islib') and contains(@class, 'nfEiy')]")

    # Retrieve the href attribute of the <a> tag
    href = link_element.get_attribute("href")

    if href:
        # Print the href attribute
        print("Href:", href)
    else:
        print("Href attribute is empty for the element.")

except Exception as e:
    print("An error occurred:", e)
driver.quit()

问题原因与解决方案

核心原因

  1. 页面渲染不及时:发送搜索请求后直接定位元素,<a>标签的href可能还未被JavaScript动态生成,导致获取到None。
  2. 定位选择器不稳定:谷歌图片的class名称会频繁更新,依赖固定class组合的XPath容易失效。

修改后的代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

chrome_options = webdriver.ChromeOptions()

driver = webdriver.Chrome(options=chrome_options)
driver.maximize_window()
driver.get("https://www.google.com/search?q=5285000201168&sca_esv=560946880&tbm=isch&sxsrf=AB5stBi24FB3-HvM3lZXc7ICrUqeHo3rAQ%3A1693299850492&source=hp&biw=1920&bih=995&ei=irTtZIeVG-PHkdUP96OoiAM&iflsig=AD69kcEAAAAAZO3CmjYgqhpcqRhnPykrz3kO7zPj6_kn&ved=0ahUKEwiHgtbAwYGBAxXjY6QEHfcRCjEQ4dUDCAc&uact=5&oq=5285000201168&gs_lp=EgNpbWciDTUyODUwMDAyMDExNjgyBBAjGCdIwQdQ3wRY3wRwAXgAkAEAmAHBAaABwQGqAQMwLjG4AQPIAQD4AQL4AQGKAgtnd3Mtd2l6LWltZ6gCCsICBxAjGOoCGCc&sclient=img")

# 等待搜索框加载完成再操作
search_field = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, 'REsRA'))
)
search_field.clear()
search_field.send_keys("5285000206613")
search_field.send_keys(Keys.ENTER)

try:
    # 等待第一张图片的<a>标签加载,使用更稳定的选择器
    link_element = WebDriverWait(driver, 15).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "div.islrc a:first-of-type"))
    )
    
    # 优先获取href属性,失败则尝试property
    href = link_element.get_attribute("href") or link_element.get_property("href")
    
    if href:
        print("Href:", href)
    else:
        print("未获取到有效href属性")

except Exception as e:
    print("错误信息:", str(e))
finally:
    driver.quit()

关键调整

  • 添加显式等待:用WebDriverWait确保元素完全渲染后再操作,避免因页面加载未完成导致的属性为空。
  • 更换定位选择器:用div.islrc a:first-of-type替代依赖多个class的XPath,适配谷歌页面的class更新,稳定性更强。
  • 双重属性获取:同时尝试get_attribute()和get_property(),覆盖动态属性的不同渲染场景。

内容的提问来源于stack exchange,提问作者Shoto Venkatesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 12:24:57