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

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

问题描述

直到几天前,下面的代码才起作用,转到 url,找到表格并将表格内容导入 Excel.然后我做了一些其他的格式化,将表格放入适当的行和列.但是现在此代码无法定位该表.我不完全理解Set 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__QigQ9cJV03ohjQ9cJV03ohjQ9cJV03ohjQ9cJV03ohjQ9cJV03ohjwkeK5JbXtgh7yng2UkeK5JbXgh7yng2UkeK5Jbxh8hjwk5uxtgh7dmjfgw1bxhdmfg201010101010101011=817544912&single=true&chrome=false&widget=false&headers=false" url.*注意:我不使用此 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,然后您就可以在该文档中与其内容进行交互.无需使用 Selenium 对它进行 .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 的网页上使用 selenium 和 vba 查找表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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