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

Selenium无法定位网站视频播放器元素问题求助

问题描述

我在对网站https://www.wcofun.net/adventure-time-season-7-episode-24-the-hall-of-egress做自动播放自动化时,脚本能找到下一集按钮,但定位不到视频控制器元素(比如类名为vjs-quality-container的画质控制器)。尝试用move_by_offset时触发了越界异常,用Xpath定位也无效。

相关代码如下:

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

chrome_bin = "C:/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe"
options = webdriver.ChromeOptions()
options.binary_location = chrome_bin
options.add_argument("--start-maximized")

driver = webdriver.Chrome(options=options)
driver.get("https://www.wcofun.net/adventure-time-season-7-episode-24-the-hall-of-egress")

div_element = driver.find_element(By.CLASS_NAME, "prev-fln")

# 查找div内class为prev-next且style包含float:right的span
span_element = div_element.find_element(By.XPATH, "//span[@class='prev-next' and contains(@style,'float:right')]")

# 查找span内的a标签
a_element = span_element.find_element(By.TAG_NAME, "a")

a_element.click()
driver.execute_script("window.scrollBy(0, 710);")
time.sleep(3)

# 实例化ActionChains执行鼠标移动点击
actions = ActionChains(driver)
x_coord = 590
y_coord = 381
actions.move_by_offset(x_coord, y_coord).click().perform()

# 再次实例化ActionChains
actions = ActionChains(driver)
x_coord = 966
y_coord = 559

# 尝试定位画质控制器元素
div_elementa = driver.find_element(By.CLASS_NAME, "vjs-quality-container")

time.sleep(1000)

目标元素结构:

<div class="vjs-quality-container"><div class="vjs-quality-dropdown"><ul><li data-code="hd720"><a href="#">HD</a></li><li data-code="hd576"><a href="#">SD</a></li></ul></div><button class="vjs-brand-quality-link">Quality</button></div>
解决思路与修正方案
  • 解决元素不可见/未加载问题:视频控制器通常为悬浮显示,需先触发视频区域的交互(如鼠标移上去)才会显示。同时用显式等待替代固定time.sleep,确保元素加载完成后再操作,避免因加载延迟导致定位失败。
  • 替换绝对坐标定位:move_by_offset依赖固定屏幕坐标,窗口尺寸变化就会失效,改为定位视频容器元素后,通过move_to_element触发控制器显示,适配不同窗口大小。
  • 修正Xpath范围:原Xpath是全局搜索,改为相对路径(.//开头),限定在父元素内查找,避免匹配到其他无关元素。

修正后的代码示例:

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

chrome_bin = "C:/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe"
options = webdriver.ChromeOptions()
options.binary_location = chrome_bin
options.add_argument("--start-maximized")

driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 15)  # 设置15秒超时等待

# 打开目标页面并等待加载
driver.get("https://www.wcofun.net/adventure-time-season-7-episode-24-the-hall-of-egress")

# 定位下一集按钮(使用相对Xpath避免全局匹配)
div_element = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "prev-fln")))
span_element = div_element.find_element(By.XPATH, ".//span[@class='prev-next' and contains(@style,'float:right')]")
a_element = span_element.find_element(By.TAG_NAME, "a")
a_element.click()

# 等待视频容器加载并滚动到视图中
video_container = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "video-js")))
driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", video_container)

# 移动鼠标到视频容器,触发悬浮控制器显示
actions = ActionChains(driver)
actions.move_to_element(video_container).perform()

# 等待画质控制器可见并点击按钮
quality_container = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "vjs-quality-container")))
quality_button = quality_container.find_element(By.CLASS_NAME, "vjs-brand-quality-link")
quality_button.click()

# 等待画质选项加载并选择HD
hd_option = wait.until(EC.visibility_of_element_located((By.XPATH, ".//li[@data-code='hd720']/a")))
hd_option.click()

time.sleep(10)
driver.quit()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 00:13:23