【问题标题】:VBA Selenium - Extract infos from aria-labelVBA Selenium - 从 aria-label 中提取信息
【发布时间】:2021-12-04 13:25:06
【问题描述】:

我正在开发一个 VBA 项目,我的目标是在 aria-label 中获取文本。我尝试了以下 VBA 代码,但没有成功:

Set hr_ida = driver.FindElementByClass("wtdjmc YMlIz ogfYpf tPgKwe")

MsgBox hr_ida.Attribute("aria-label")

HTML:

<div class="wtdjmc YMlIz ogfYpf tPgKwe" aria-label="Horário de partida: 19:35.">19:35</div>

【问题讨论】:

  • 这里是 HTML:
    19:35
  • 请记住,引号中有 4 个不同的类,而不仅仅是其中有空格的 1 个类。尝试只使用其中 1 个而不是全部 4 个,因为我不认为 FindElementByClass 那样工作。
  • 另外请记住,类可以被多个元素使用,因此仅基于这一点,如果有一个名为FindElementByClass(单数)的实际函数,我会感到惊讶——它很可能是@ 987654326@(复数)(但我不熟悉 Selenium)
  • 你能显示更多的html/url吗?这些看起来像动态值,根本不应该依赖(xpath 或 css)。

标签: html vba selenium google-chrome web


【解决方案1】:

我认为当有多个类时使用xpath 会更好。试试下面的

Dim hr_ida As Selenium.WebElement

Set hr_ida = .FindElementByXPath("//div[@class='wtdjmc YMlIz ogfYpf tPgKwe']")
MsgBox hr_ida.Attribute("aria-label")

或者像这样使用css

Dim hr_ida As Selenium.WebElement

Set hr_ida = .FindElementByCss(".wtdjmc.YMlIz.ogfYpf.tPgKwe")
MsgBox hr_ida.Attribute("aria-label")

【讨论】:

    【解决方案2】:

    设置 hr_ida = driver.FindElementByClass("wtdjmc YMlIz ogfYpf tP​​gKwe")

    不允许使用复合类名称,并且您无法使用这些类型的类名称找到元素。试试下面xPaths

    Set hr_ida = driver.FindElementByXPath("//div[@class='wtdjmc YMlIz ogfYpf tPgKwe']")
    

    Set hr_ida = driver.FindElementByXPath("//div[contains(@aria-label,'Horário de partida:')]")
    

    【讨论】:

      猜你喜欢
      • 2020-05-09
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多