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

如何使用Selenium点击页面标签以抓取世界杯赛事统计数据?

问题分析与解决方法

赛事统计标签页截图

你遇到的点击「Stats」标签失败的问题,并非XPath本身不可行,而是大概率出在绝对XPath不稳定、元素加载时机、headless模式的限制或者未处理页面遮罩层这几个方面,以下是针对性的修复方案:

1. 替换绝对XPath为相对XPath,提升稳定性

绝对XPath(如/html/body/div[1]/...)一旦页面结构微调就会失效,建议改用基于元素文本或稳定属性的相对定位:

# 优先通过data-testid定位,无则用文本定位Stats标签
elem = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//*[@data-testid='tab-stats' or text()='Stats']"))
)
elem.click()

2. 确保页面完全加载后再操作

你当前获取球队信息的代码未等待页面加载,直接调用find_element容易出现元素未渲染的问题,建议先等待核心区域加载完成:

driver.get(url)
# 等待比分区域加载,确认页面基础结构就绪
WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, "//div[contains(@class, 'match-header__score')]"))
)
# 改用相对路径获取球队和比分
HomeTeam = driver.find_element(By.XPATH, "//div[@class='match-header__team--home']//p[@class='match-header__team-name']").text
AwayTeam = driver.find_element(By.XPATH, "//div[@class='match-header__team--away']//p[@class='match-header__team-name']").text
Result = driver.find_element(By.XPATH, "//div[@class='match-header__score']").text

3. 修复headless模式的视窗限制

headless模式默认窗口尺寸较小,可能导致Stats标签被隐藏在滚动区域或响应式菜单中,需手动设置桌面级窗口尺寸:

option = Options()
option.headless = True
option.add_argument("--window-size=1920,1080")  # 设置适配桌面端的窗口大小
driver = webdriver.Chrome("C:/Users/paulc/Documents/Medium Football/chromedriver.exe", options=option)

4. 提前处理页面遮罩层

页面的Cookie同意弹窗、加载遮罩会阻断点击操作,需先处理这类元素:

# 处理Cookie同意弹窗(根据实际页面按钮文本调整)
try:
    cookie_btn = WebDriverWait(driver, 5).until(
        EC.element_to_be_clickable((By.XPATH, "//button[text()='Accept All']"))
    )
    cookie_btn.click()
except:
    pass  # 无弹窗则跳过

完整修复后的代码片段

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from datetime import datetime
import pandas as pd
import warnings
import os

warnings.filterwarnings('ignore')

url = 'https://www.fifa.com/fifaplus/en/match-centre/match/17/255711/285063/400128082?country=IE&wtw-filter=ALL'

option = Options()
option.headless = True
option.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome("C:/Users/paulc/Documents/Medium Football/chromedriver.exe", options=option)
driver.get(url)

# 处理Cookie弹窗
try:
    cookie_btn = WebDriverWait(driver, 5).until(
        EC.element_to_be_clickable((By.XPATH, "//button[text()='Accept All']"))
    )
    cookie_btn.click()
except:
    pass

# 等待页面核心元素加载
WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, "//div[@class='match-header__score']"))
)

# 获取球队与比分数据
HomeTeam = driver.find_element(By.XPATH, "//div[@class='match-header__team--home']//p[@class='match-header__team-name']").text
AwayTeam = driver.find_element(By.XPATH, "//div[@class='match-header__team--away']//p[@class='match-header__team-name']").text
Result = driver.find_element(By.XPATH, "//div[@class='match-header__score']").text

# 点击Stats标签
elem = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//*[@data-testid='tab-stats' or text()='Stats']"))
)
elem.click()

# 后续编写统计数据抓取代码...

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 16:10:28