【问题标题】:How to find a table using selenium and vba on webpage that uses iframes?如何在使用 iframe 的网页上使用 selenium 和 vba 查找表格?
【发布时间】:2021-09-26 18:27:43
【问题描述】:

下面的代码直到几天前才运行到 url,找到表格并将表格的内容导入 Excel。然后我做了一些其他的格式化来让表格进入适当的行和列。但是现在这段代码找不到表。我不完全理解“Set a = .FindElementsByTag("iframe")(2)”和“.SwitchToFrame 1”。但我的一般理解是,这部分代码切换到不同的框架,然后提取内部 url,然后用于从表中获取数据。

我需要帮助确定要更改的内容才能获得预期的“url2”,即“https://docs.google.com/spreadsheets/d/e/2PACX-1vT__QigQ9cJV03ohUkeK5dgQjfAbJqxrc68bXh9Is1WFST8wjxMxDy7hYUCFHynqRvInsANUI22GdIM/pubhtml?gid=817544912&single=true&chrome=false&widget=false&headers=false”url。 *注意:我不使用这个 docs.google 网址,因为我不知道这个网址是否会定期更改。我知道 rosterresource.com/mlb-roster-grid 网址将保持一致。

我尝试更改“Set a = .FindElementsByTag("iframe")(2)”和“.SwitchToFrame 1”的一些整数,但我这样做是盲目的,因为我不熟悉这种艺术代码。

Sub GetRRgrid()
    '"Selenium type library" is a reference used
    Dim d As WebDriver, a As Object
    Set d = New ChromeDriver
    Const url = "https://www.rosterresource.com/mlb-roster-grid/"

    With d
        .Start "Chrome"
        .Get url

        Set a = .FindElementsByTag("iframe")(2)

        .SwitchToFrame 1

        url2 = .FindElementByCss("iframe").Attribute("src")
        .Get url2
        ele = .FindElementByTag("tbody").Attribute("innerText")
        d.Close
    End With
    ' other processes t format the data after it is imported
end sub
````

【问题讨论】:

  • 因此,将d.FindElementsByTag("iframe") 想象为对象的集合。更具体地说,它是 HTML 文档中所有 <iframe></iframe> 元素的集合。要访问这些元素中的每一个,请使用 (i) 符号。所以 d.FindElementsByTag("iframe")(2) 是这个集合中的第三个对象。
  • 你知道我应该使用哪个 (i) 符号吗? .attribute("src") 应该等于 "docs.google.com/spreadsheets/d/e/…"

标签: excel vba selenium iframe web-scraping


【解决方案1】:

获取 iframe 并切换到它:

您需要将 iframe 元素(identifier 参数)传递给SwitchToFrame,然后您就在该文档中并且可以与其内容进行交互。无需 .get 使用 Selenium。您必须切换到.SwitchToDefaultContent 才能返回父文档。

您可以通过多种方式识别相关 iframe。现代浏览器针对 css 选择器进行了优化,所以我通常使用这些选择器。

的 css 等价物
.FindElementByTag("iframe")

.FindElementByCss("iframe")

您的 iframe 是第一个(也是唯一一个),因此我不会费心收集一组 webElement 并对其进行索引。此外,您想尽可能地尝试使用单个元素的短选择器以提高效率。


VBA:

Option Explicit
Public Sub Example()
    Dim d As WebDriver
    Const URL As String = "https://www.rosterresource.com/mlb-roster-grid/"
    Set d = New ChromeDriver

    With d
        .Start "Chrome"
        .get URL

        .SwitchToFrame .FindElementByCss("iframe")

        Stop

        .Quit
    End With
End Sub

写入 Excel (.AsTable.ToExcel):

我刚刚发现,还没有在任何地方看到文档记录,我很兴奋的是,有一种方法可以将表格直接写入 Excel:

Option Explicit
Public Sub Example()
    Dim d As WebDriver
    Const URL As String = "https://www.rosterresource.com/mlb-roster-grid/"
    Set d = New ChromeDriver

    With d
        .Start "Chrome"
        .get URL

        .SwitchToFrame .FindElementByTag("iframe")
        .FindElementByCss(".waffle").AsTable.ToExcel ThisWorkbook.Worksheets("Sheet1").Range("A1")
        Stop

        .Quit
    End With
End Sub

【讨论】:

  • 这个方法看起来很有趣,并且可以帮助我将它作为表格写入 Excel。但是当我到达“.FindElementByCss(".waffle").AsTable.ToExcel”时出现错误。 “NoSuchElementError。未找到 Css= .waffle 的元素”。
  • 检查网页中表格的类名。对我来说似乎是这样。您也可以使用 .FindElementByCss("table").AsTable.ToExcel
  • 你确定你也切换到 iframe 了吗?
  • 我接受了顶部。自从我到达那个 iframe 之后,这似乎有效(没有错误消息)。即使我不确定这是否是正确的 iframe。我怎么知道我在 iframe 中是正确的?我仍然无法让这部分工作:.FindElementByCss(".waffle").AsTable.ToExcel ThisWorkbook.Worksheets("Sheet1").Range("A1")。它找不到那个 .waffle 元素。
  • .FindElementByCss("table").AsTable.ToExcel 会发生什么?很奇怪,因为课程肯定在那里,并且每次我运行它时都有效。您也可以尝试在之前添加等待,但这确实不应该有所作为。
【解决方案2】:

这就是我最终为这个问题所做的。感谢 QHarr 的指导。

Public Sub GetRRrostergrid()
    Dim d As WebDriver
    Const URL As String = "https://www.rosterresource.com/mlb-roster-grid/"
    Dim URL2 As String
    Set d = New ChromeDriver
    Sheet20.Activate

    With d
        .Start "Chrome"
        .Get URL
        URL2 = .FindElementByClass("post_content").FindElementByTag("iframe").Attribute("src")
        .Get URL2
        .FindElementByCss(".waffle").AsTable.ToExcel ThisWorkbook.Worksheets("RRchart").Range("b1")
        .Quit
    End With
End Sub

【讨论】:

    猜你喜欢
    • 2021-12-30
    • 1970-01-01
    • 2018-01-21
    • 2021-05-12
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多