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

Selenium VBA:如何通过元素ID判断所属球员行类型

解决方法

核心逻辑

通过已定位的元素(如aj_1_7_shirtNumber)逐层向上遍历DOM结构,找到其所属的player-row父元素,再读取该父元素的class属性,判断是否包含p_starter或p_notstarter标识。

VBA代码实现

Set MyVal = ch.FindElementById("aj_1_7_shirtNumber")
Sht.Cells(1, 1) = MyVal.Text

' 向上遍历找到所属的player-row父元素
Dim playerRow As Object
Set playerRow = MyVal.Parent
Do While Not playerRow Is Nothing And InStr(playerRow.Attribute("class"), "player-row") = 0
    Set playerRow = playerRow.Parent
Loop

' 判断并写入行类型
Dim rowType As String
If Not playerRow Is Nothing Then
    If InStr(playerRow.Attribute("class"), "p_starter") > 0 Then
        rowType = "p_starter"
    ElseIf InStr(playerRow.Attribute("class"), "p_notstarter") > 0 Then
        rowType = "p_notstarter"
    Else
        rowType = "未知类型"
    End If
    Sht.Cells(1, 2) = rowType
Else
    Sht.Cells(1, 2) = "未找到所属行元素"
End If

代码说明

  • 依赖DOM元素的Parent属性逐层向上查找,完全避开了表格序号变动的问题,仅基于固定元素ID关联父级行元素。
  • 使用InStr函数检查class属性内容,精准匹配目标标识类名。
  • 增加空值判断逻辑,避免因DOM结构变动导致的运行错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 11:57:33