如何在使用iframe的网页上使用硒和vba查找表? [英] How to find a table using selenium and vba on webpage that uses iframes?

查看:145
本文介绍了如何在使用iframe的网页上使用硒和vba查找表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到几天前,下面的代码都可以工作到URL,找到表并将表的内容导入Excel.然后,我进行了其他一些格式化,以将表放入适当的行和列中.但是现在此代码无法找到表.我不完全了解设置a = .FindElementsByTag("iframe")(2)"和".SwitchToFrame 1".但是我的一般理解是,这部分代码切换到另一个框架,该框架然后提取内部url,然后将其用于从表中获取数据.

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.

我需要帮助确定要进行哪些更改才能获得预期的"url2",即" https://docs.google.com/spreadsheets/d/e/2PACX-1vT__QigQ9cJV03ohUkeK5dgQjfAbJqxrc6y = 817544912& single = true& chrome = false& widget = false&headers = false "网址. *注意:我不使用此docs.google网址,因为我不知道此网址是否会定期更改.我知道rosterresource.com/mlb-roster-grid网址会保持一致.

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.

我曾尝试更改"Set a = .FindElementsByTag("iframe")(2)"和".SwitchToFrame 1"的一些整数,但由于我对这种艺术不熟悉,所以我会盲目地这样做.代码.

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
````

推荐答案

获取iframe并切换到它:

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

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.

您可以通过多种方式识别相关的iframe.现代浏览器针对CSS选择器进行了优化,因此我通常会使用它们.等同于

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")

.FindElementByCss("iframe")

您的iframe是第一个(也是唯一一个),因此我不会费心收集一组webElement并将其编入索引.另外,您希望尝试使用单个元素的简短选择器,以提高效率.

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


写入Excel(.AsTable.ToExcel):


Writing to Excel (.AsTable.ToExcel) :

我只是发现了一种方法,可以将表直接写到Excel:

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

这篇关于如何在使用iframe的网页上使用硒和vba查找表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆