excel Selenium-vba通过类名获取元素

yruzcnhs  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(147)

当我尝试下面的我得到一个错误“对象不支持属性或方法”

Sub Testing()
   Dim driver As New SeleniumWrapper.WebDriver
   driver.Start "chrome", "http://www.tsn.ca/fury-upsets-klitschko-to-become-heavyweight-champion-1.401257"
   driver.Open "/"

   MsgBox driver.getElementsByClassName("headline").Text

End Sub

我也试过driver.getElementsByClassName("headline")(0).Text

anauzrmj

anauzrmj1#

不是“得到”,而是“发现”:

driver.findElementByClassName("headline").Text

或者,使用CSS选择器:

driver.findElementByCssSelector(".headline").Text
lbsnaicq

lbsnaicq2#

website中的headline元素是:

<div class="headline">
    <h1>Fury upsets Klitschko to win heavyweight titles</h1>
</div>

因此,您可以使用以下定位器策略之一:

  • 使用 FindElementByClassName
driver.FindElementByClassName("headline").Text
  • 使用 FindElementByCss
driver.FindElementByCss("div.headline > h1").Text
  • 使用 FindElementByXPath
driver.FindElementByXPath("//div[@class='headline']/h1").Text

相关问题