首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Selenium WebDriver vba从隐藏元素中获取文本

使用Selenium WebDriver vba从隐藏元素中获取文本
EN

Stack Overflow用户
提问于 2021-03-17 20:28:30
回答 1查看 373关注 0票数 0

我试图将https://www.soccerstats.com/matches.asp?matchday=3中的值解析到我的Excel中,所以我使用了VBA。但是当我尝试解析来自隐藏元素的数据时,例如

我正在做一个循环,在那里我运行所有的游戏,只有搜索游戏,每队玩超过5次。但是,当我没有输入这个隐藏元素时,我的代码工作得很好。有人能帮助我如何在有隐藏元素或没有隐藏元素的元素中输入我的if help吗?

代码语言:javascript
复制
For i = 1 To .FindElementsByXPath("//*[@class='steam']/../td[@class='sgray'][2]").Count Step 2
           
           
           If IsNumeric(.FindElementByXPath("(//*[@class='steam']/../td[@class='sgray'][2])[" & i & "]").Text) And IsNumeric(.FindElementByXPath("(//*[@class='steam']/../td[@class='sgray'][2])[" & i & "]").Text) Then
               
               If CInt(.FindElementByXPath("(//*[@class='steam']/../td[@class='sgray'][2])[" & i & "]").Text) > 5 And CInt(.FindElementByXPath("(//*[@class='steam']/../td[@class='sgray'][2])[" & i & "]").Text) > 5 Then
                  
                  league = .FindElementByXPath("((((//*[@class='steam']/../td[@class='sgray'][2])[" & i & "]/../td//preceding::tr[@class='parent'])[last()])/td/font)[1]").Text

                   
                  If Not IsError(Application.Match(league, Worksheets("Ligas").Range("E:E"), 0)) Then
                      'Buscar dados se o numero de jogos for numérico
                      'Encontrar nome da liga
                      
                      hometeam = .FindElementByXPath("(//*[@class='steam']/../td[@class='sgray'][2])[" & i & "]/../td[1]").Text
                      awayteam = .FindElementByXPath("(//*[@class='steam']/../td[@class='sgray'][2])[" & i + 1 & "]/../td[1]").Text
                   
                      homegames = .FindElementByXPath("(//*[@class='steam']/../td[@class='sgray'][2])[" & i & "]").Text
                      awaygames = .FindElementByXPath("(//*[@class='steam']/../td[@class='sgray'][2])[" & i + 1 & "]").Text
                      
                      homegoalsscored = .FindElementByXPath("(//*[@class='steam']/../td[@class='sgray'][2])[" & i & "]/following-sibling::td[@class='sgreen'][2]").Text
                      awaygoalsscored = .FindElementByXPath("(//*[@class='steam']/../td[@class='sgray'][2])[" & i + 1 & "]/following-sibling::td[@class='sgreen'][2]").Text
                                      
                      homegoalsconceded = .FindElementByXPath("(//*[@class='steam']/../td[@class='sgreen'][2])[" & i & "]/following-sibling::td[@class='sred'][1]").Text
                      awaygoalsconceded = .FindElementByXPath("(//*[@class='steam']/../td[@class='sgreen'][2])[" & i + 1 & "]/following-sibling::td[@class='sred'][1]").Text
                      
                      gametime = .FindElementByXPath("(//*[@class='steam']/../td[@class='sgray'][2])[" & i & "]/../td[@valign='middle']/font").Text
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-17 22:51:12

如果测试给定节点的.Attribute("style"),返回值可以确定是否显示:none;

例如伪码

代码语言:javascript
复制
If <node>.Attribute("style") = "display:none ;" Then
    ' do something....
Else
   'do something different
End If

您还可以使用Instr测试子字符串。

代码语言:javascript
复制
If Instr(<node>.Attribute("style"), "display:none")  > 0 Then
    ' do something....
Else
   'do something different
End If

<node>是当前迭代中的节点/元素的占位符,无论测试需要什么级别;从您的问题中看不清楚。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66680756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档