使用Selenium VBA从网页复制表格

时间:2018-06-15 16:17:12

标签: excel vba selenium-webdriver

我正在尝试将此网页(https://www.rosterresource.com/mlb-schedule-grid/)中的图表复制到Excel中的工作表中。我一直在使用带有Selenium参考的VBA来访问网页并尝试按Xpath或ID查找元素,但是我在查找图表时遇到了问题。我认为我要复制的图表位于不同的框架中,但我不熟悉如何正确使用它。

Dim driver As Selenium.ChromeDriver
Set driver = New Selenium.ChromeDriver
Dim url As String

url = "https://www.rosterresource.com/mlb-schedule-grid/"

driver.Start "chrome", "https://www.google.com/"
driver.Wait 1000
driver.Get url
driver.Wait 5000
Element = driver.FindElementByXPath(???).Attribute("innerText") 'this is where I struggle
driver.Close

Range("a1").Value = Element

2 个答案:

答案 0 :(得分:1)

有两个嵌套的iframe可以从该页面获取表格数据。尝试以下方式从waffle类名称下的该页面的每个表中获取所有数据。

Sub FetchTable()
    Const link As String = "https://www.rosterresource.com/mlb-schedule-grid/"
    Dim posts As Object, post As Object, elem As Object, R&, C&

    With New ChromeDriver
        .get link
        .SwitchToFrame .FindElementByCss("iframe", timeout:=5000)
        .SwitchToFrame .FindElementByCss("iframe#pageswitcher-content", timeout:=5000)
        For Each posts In .FindElementsByCss("table.waffle tr")
            For Each elem In posts.FindElementsByCss("td")
                C = C + 1: Cells(R + 1, C) = elem.Text
            Next elem
            R = R + 1: C = 0
        Next posts
        .Quit
    End With
End Sub

答案 1 :(得分:0)

您可以选择iFrame并使用以下语法切换到它们:

Option Explicit
Public Sub GetTable()
    Dim d As WebDriver, a As Object
    Set d = New ChromeDriver
    Const URL = "https://www.rosterresource.com/mlb-schedule-grid/"

    With d
        .Start "Chrome"
        .Get URL

        Set a = .FindElementsByTag("iframe")(1)  
        .SwitchToFrame 0
    End With
End Sub

但是,包含数据的电子表格的直接链接可以通过以下方式获得:

 .FindElementByCss("iframe").Attribute("src")