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

向YouTube搜索框输入文本时遭遇StaleElementReferenceException问题求助

问题描述

需要实现自动化操作:打开YouTube、接受Cookie、定位搜索框输入内容并点击搜索,但执行send_keys()时抛出错误:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

已尝试WebDriverWait、不同定位方式(XPATH/ID/CSS_SELECTOR),操作Google时正常,但YouTube上始终失败。

原代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_experimental_option('detach', True)
options.add_experimental_option('excludeSwitches', ['enable-logging'])

driver = webdriver.Chrome(options=options)
driver.get("https://www.youtube.com/")
driver.maximize_window()

cookie_accept = driver.find_element(By.XPATH, '//*[@id="content"]/div[2]/div[6]/div[1]/ytd-button-renderer[2]/yt-button-shape/button/yt-touch-feedback-shape/div/div[2]')
cookie_accept.click()

yt_searchbox = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="search"]')))
yt_searchbox.send_keys('Python Selenium')
问题原因
  1. 定位元素错误:原代码中定位的//*[@id="search"]是搜索框的外层容器(div),而非实际接收输入的<input>元素,即使找到该div,调用send_keys()也无法生效,且页面重渲染后该div可能失效。
  2. 未等待Cookie操作后的页面稳定:点击Cookie按钮后,YouTube页面可能会有局部重渲染,导致之前缓存的元素引用失效,触发StaleElementReferenceException。
  3. Cookie按钮定位未做等待:直接用find_element获取Cookie按钮,可能因弹窗加载不及时导致元素未找到或点击失败,间接引发后续页面状态异常。
解决方案

修改代码,解决上述问题:

  1. 用WebDriverWait等待Cookie按钮加载完成后再点击;
  2. 定位实际的搜索输入框(<input>元素);
  3. 点击Cookie后,重新等待搜索框稳定加载,确保元素引用有效。

修改后的代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_experimental_option('detach', True)
options.add_experimental_option('excludeSwitches', ['enable-logging'])

driver = webdriver.Chrome(options=options)
driver.get("https://www.youtube.com/")
driver.maximize_window()

# 等待并点击Cookie同意按钮(使用更稳定的定位,避免依赖冗长XPATH)
cookie_accept = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//button[contains(@aria-label, "同意") or contains(@aria-label, "Accept")]'))
)
cookie_accept.click()

# 等待实际的搜索输入框加载完成并可交互
yt_search_input = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, 'input#search'))
)
yt_search_input.send_keys('Python Selenium')

# 等待并点击搜索按钮
search_button = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, 'button#search-icon-legacy'))
)
search_button.click()
补充说明
  • 使用contains(@aria-label, ...)定位Cookie按钮,避免因页面布局变化导致XPATH失效;
  • 直接定位<input>元素确保send_keys()能正确输入内容;
  • 每一步操作前都用WebDriverWait等待元素,保证页面状态稳定,避免元素引用失效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 10:46:19