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

使用RSelenium抓取Sofascore球员统计汇总表遇元素定位失败求助

问题描述

我想抓取页面中「Player statistics」板块下的汇总表格,页面地址为:https://www.sofascore.com/southampton-wolverhampton/dsV

我尝试用RSelenium实现需求,当前代码如下:

rm=rsDriver(browser = "chrome", chromever ="111.0.5563.64",
            verbose = F,
            port = free_port())

rmDr=rm$client
rmDr$open()
rmDr$navigate("https://www.sofascore.com/southampton-wolverhampton/dsV")
elem <- rmDr$findElement(using = 'xpath', '//button[@data-tabid="summary"]')

点击「summary」按钮会显示汇总数据,但用上述XPath定位按钮失败,报错信息如下:

Selenium message:no such element: Unable to locate element: {"method":"xpath","selector":"//button[@data-tabid="summary"]"}
  (Session info: chrome=111.0.5563.65)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '4.0.0-alpha-2', revision: 'f148142cf8', time: '2019-07-01T21:30:10'
System info: host: 'DESKTOP-MOGN5AG', ip: '192.168.0.114', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '19.0.2'
Driver info: driver.version: unknown

Error:   Summary: NoSuchElement
     Detail: An element could not be located on the page using the given search parameters.
     class: org.openqa.selenium.NoSuchElementException
     Further Details: run errorDetails method

截图:
截图展示

求其他可行的定位方法,感谢帮助。


解决方法

针对定位失败的问题,你可以从以下几个方向调整代码:

1. 等待元素加载完成

页面可能还未完全加载就执行了查找元素的操作,导致定位失败。可以用显式等待,等待元素出现后再操作:

library(RSelenium)
library(wdman)

rm <- rsDriver(browser = "chrome", chromever = "111.0.5563.64", verbose = F, port = free_port())
rmDr <- rm$client
rmDr$navigate("https://www.sofascore.com/southampton-wolverhampton/dsV")

# 等待最多10秒,直到目标元素可被定位
elem <- rmDr$waitForElement(using = 'xpath', '//button[@data-tabid="summary"]', timeout = 10000)
elem$click()

2. 修正元素定位表达式

原XPath可能因页面结构变化失效,试试这些定位方式:

  • 按按钮文本定位:如果按钮显示文本为「Summary」,用文本匹配的XPath:
    elem <- rmDr$findElement(using = 'xpath', '//button[text()="Summary"]')
    
  • 用CSS选择器定位:
    elem <- rmDr$findElement(using = 'css selector', 'button[data-tabid="summary"]')
    
  • 检查iframe嵌套:如果目标元素在iframe内,需要先切换到iframe再查找:
    # 定位并切换到目标iframe(需根据页面实际结构调整选择器)
    iframe <- rmDr$findElement(using = 'css selector', 'iframe[src*="player-stats"]')
    rmDr$switchToFrame(iframe)
    # 再定位按钮
    elem <- rmDr$findElement(using = 'xpath', '//button[@data-tabid="summary"]')
    

3. 先处理Cookie弹窗

页面的Cookie同意弹窗可能干扰元素加载或定位,先关闭弹窗:

# 等待弹窗加载并点击同意按钮(根据实际弹窗调整选择器)
cookie_btn <- rmDr$waitForElement(using = 'css selector', 'button[aria-label="Accept"]', timeout = 10000)
cookie_btn$click()

4. 匹配ChromeDriver版本

你的Chrome浏览器版本是111.0.5563.65,但代码中指定的chromever是111.0.5563.64,版本不一致可能导致兼容性问题。可以改成对应版本,或者让RSelenium自动匹配最新兼容版本:

rm <- rsDriver(browser = "chrome", chromever = "latest", verbose = F, port = free_port())

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 19:24:54