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

如何用Selenium+Chrome定位Hugging Face空间的文本输入框并输入提示词

解决Hugging Face文本生成视频空间输入框定位超时问题

可能的原因及对应解决方案

1. 元素嵌套在iframe中

部分Hugging Face Spaces的交互组件会嵌套在iframe内,需先切换到对应iframe再定位元素:

# 定位并切换到iframe
iframe = WebDriverWait(driver, 20).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, "iframe"))
)
driver.switch_to.frame(iframe)

# 定位输入框并输入内容
text_input = WebDriverWait(driver, 20).until(
    EC.element_to_be_clickable((By.ID, "prompt"))
)
text_input.clear()
text_input.send_keys(video_prompt)

# 操作完成后可切回主文档(如果需要后续操作其他元素)
# driver.switch_to.default_content()

2. 定位表达式错误

你之前使用的定位路径不准确,实际页面中输入框的直接ID为prompt,可使用更精准的定位方式,同时优先等待元素可交互而非仅存在:

# CSS选择器方式
text_input = WebDriverWait(driver, 30).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, "input#prompt"))
)

# XPATH方式
# text_input = WebDriverWait(driver, 30).until(
#     EC.element_to_be_clickable((By.XPATH, "//input[@id='prompt']"))
# )

text_input.clear()
text_input.send_keys(video_prompt)

3. 元素未处于可视区域

页面加载后输入框可能不在当前可视范围内,需先滚动到元素位置再操作:

text_input = WebDriverWait(driver, 30).until(
    EC.presence_of_element_located((By.ID, "prompt"))
)
# 滚动到元素可见
driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", text_input)
# 等待元素可点击
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "prompt")))

text_input.clear()
text_input.send_keys(video_prompt)

4. 页面检测自动化工具

部分空间会检测Selenium的自动化特征,可通过配置浏览器参数规避:

from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option("useAutomationExtension", False)

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://huggingface.co/spaces/damo-vilab/modelscope-text-to-video-synthesis")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 09:54:58