Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

excel - How to find a table using selenium and vba on webpage that uses iframes?

The below code worked up until a few days ago to go to the url, find the table and import the contents of the table into Excel. I then did some other formatting to get the table into the appropriate rows and columns. But now this code cannot locate the table. I do not fully understand the "Set a = .FindElementsByTag("iframe")(2)" and the ".SwitchToFrame 1". But my general understanding is that this portion of the code switches to a different frame which then extracts the internal url, which then is used to get the data form the table.

I need help identifying what to change in order to get the intended "url2", which is "https://docs.google.com/spreadsheets/d/e/2PACX-1vT__QigQ9cJV03ohUkeK5dgQjfAbJqxrc68bXh9Is1WFST8wjxMxDy7hYUCFHynqRvInsANUI22GdIM/pubhtml?gid=817544912&single=true&chrome=false&widget=false&headers=false" url. *note: I do not use this docs.google url because I do not know if this url will change periodically. I know the rosterresource.com/mlb-roster-grid url will stay consistent.

I have tried changing some of the integers for "Set a = .FindElementsByTag("iframe")(2)" and the ".SwitchToFrame 1", but I am doing that blindly since I am not familiar with this art of the code.

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
````
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Getting the iframe and switching to it:

You need to pass the iframe element (identifier argument) to SwitchToFrame, you are then within that document and can interact with its contents. No need to .get on that with Selenium. You have to switch to .SwitchToDefaultContent to go back to parent document.

You can identify the iframe in question in a number of ways. Modern browsers are optimized for css selectors so I usually go with those. The css equivalent of

.FindElementByTag("iframe")

is

.FindElementByCss("iframe")

Your iframe is the first (and only) so I wouldn't bother gathering a set of webElements and indexing into it. Also, you want to try for a short selector of a single element where possible to be more efficient.


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

Writing to Excel (.AsTable.ToExcel) :

Something I only just discovered, haven't seen documented anywhere, and am excited by, is that there is a method to write the table direct to 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...