简体   繁体   English

使用Selenium VBA将动态网页表中的单元格值带到excel单元格

[英]Using selenium VBA bringing the cell values from a dynamic web page table to excel cells

I am new to selenium VBA and post googling have crated the below code in order to bring the each cell value from a dynamic webpage using the selenium vba. 我是Selenium VBA的新手,并且在Google谷歌搜索后创建了以下代码,以便使用Selenium VBA从动态网页中获取每个单元格的值。 I am getting error in receiving the web elements in a web_tr and web_td web elements 我在接收web_tr和web_td Web元素中的Web元素时遇到错误

Have trying with this line: 尝试使用此行:

ActiveSheet.Cells(i, 1).Value = selenium.findElementByXPath("//table[@id='ctl00_cphContentBody_itemsTable']/tbody/tr['i']/td[1]").Text

I am getting a header line item first value only though it is under the loop. 我得到的是标题行项目的第一个值,尽管它位于循环中。

Web_tr and web_td are web elements and receiving the web elements to these variables are the issue here. Web_tr和web_td是Web元素,在这里接收到这些变量的Web元素是问题。 Kindly assist. 请协助。


With ThisWorkbook
        .Sheets("UpfrontOrder#").Activate

       Set web_table = selenium.findElementsByXPath("//table[@id='ctl00_cphContentBody_itemsTable']/tbody")
       Set web_tr = web_table.findElementsByTagName("tr")   '***** the error pop-up gets in here****
       row_count = selenium.findElementsByXPath("//table[@id='ctl00_cphContentBody_itemsTable']/tbody/tr").Count

       For i = 1 To row_count
            Set web_td = web_tr.findElementsByTagName("td")
            ActiveSheet.Cells(i, 1).Value = web_td(1).getText

            'ActiveSheet.Cells(i, 1).Value = selenium.findElementByXPath("//table[@id='ctl00_cphContentBody_itemsTable']/tbody/tr['i']/td[1]").Text
            ActiveSheet.Cells(i, 2).Value = selenium.findElementByXPath("//table[@id='ctl00_cphContentBody_itemsTable']/tbody/tr['i']/td[2]").Text
            ActiveSheet.Cells(i, 3).Value = selenium.findElementByXPath("//table[@id='ctl00_cphContentBody_itemsTable']/tbody/tr['i']/td[3]").Text
            ActiveSheet.Cells(i, 4).Value = selenium.findElementByXPath("//table[@id='ctl00_cphContentBody_itemsTable']/tbody/tr['i']/td[4]").Text
            ActiveSheet.Cells(i, 5).Value = selenium.findElementByXPath("//table[@id='ctl00_cphContentBody_itemsTable']/tbody/tr['i']/td[5]").Text
            ActiveSheet.Cells(i, 6).Value = selenium.findElementByXPath("//table[@id='ctl00_cphContentBody_itemsTable']/tbody/tr['i']/td[6]").Text
            ActiveSheet.Cells(i, 7).Value = selenium.findElementByXPath("//table[@id='ctl00_cphContentBody_itemsTable']/tbody/tr['i']/td[7]").Text

            'ActiveSheet.Cells(i, 1).Value = web_td.findElementByXPath("td[1]").Text
            'ActiveSheet.Cells(i, 2).Value = web_td(1).Text

        Next i
End With

I'm not really familiar with the VBA version of Selenium but from what I can tell, I see one issue that I think is causing the error you are getting. 我对VBA版本的Selenium并不是很熟悉,但是据我所知,我认为有一个问题引起了您遇到的错误。 When you use .findElements* (plural), it will return a collection of elements instead of just a single element. 当您使用.findElements* (复数)时,它将返回元素的集合,而不仅仅是单个元素。 Your first line (below) is using .findElements() but in the second line (below) is not specifying which element of the web_table collection to reference. 您的第一行(下面)使用.findElements()但第二行(下面)没有指定要引用的web_table集合中的哪个元素。

Set web_table = selenium.findElementsByXPath("//table[@id='ctl00_cphContentBody_itemsTable']/tbody")
Set web_tr = web_table.findElementsByTagName("tr")   '***** the error pop-up gets in here****

You can try to do one of two fixes... 您可以尝试执行以下两种修复方法之一...

  1. Change .findElements() to .findElement() so that you only grab the first (and probably only?) table. .findElements()更改为.findElement()以便仅获取第一个(并且可能仅?)表。 If you only want the first one, this is the right way to fix this. 如果只想要第一个,这是解决此问题的正确方法。

  2. Add an index into the collection of elements from the first line, eg change web_table. 在第一行的元素集合中添加一个索引,例如change web_table. to web_table(1). web_table(1). If you want a TABLE tag other than the first one, then add the correct reference. 如果要使用第一个TABLE标记以外的其他TABLE标记,请添加正确的引用。

     Set web_table = selenium.findElementsByXPath("//table[@id='ctl00_cphContentBody_itemsTable']/tbody") Set web_tr = web_table(1).findElementsByTagName("tr") add this _____________^^^ 

Additional Note: I had another suggestion to simplify some of your code. 附加说明:我还有另一个建议来简化您的某些代码。 In your For i loop you have hardcoded indexes 2-7 where I think you can easily use a loop. 在您的For i循环中,您具有2-7的硬编码索引,我认为您可以轻松地使用循环。 I updated the code below. 我更新了下面的代码。 This accomplishes two things... 这完成了两件事...

  1. It eliminates the repeated scraping of the page. 它消除了页面的重复刮擦。 In your original code, you are scraping one element at a time. 在原始代码中,您一次刮取一个元素。 In the code below, I grab a collection of TD s and then iterate through them. 在下面的代码中,我获取了TD的集合,然后遍历它们。 That way I'm only scraping the page once to get all the elements I care about and then processing that collection. 这样,我只刮了一次页面即可得到我关心的所有元素,然后处理该集合。 It's more efficient. 效率更高。
  2. Simplifies the code. 简化代码。
For i = 1 To row_count
    Set web_td = web_tr.findElementsByTagName("td")
    ActiveSheet.Cells(i, 1).Value = web_td(1).getText
    Dim tds As New List
    Set tds = ActiveSheet.Cells(i, 2).Value = selenium.findElementByXPath("//table[@id='ctl00_cphContentBody_itemsTable']/tbody/tr['i']")
    For j = 2 To 7
        ActiveSheet.Cells(i, j).Value = tds(j).Text
    Next j
Next i

Thank you both of you. 谢谢你们俩

The below code works well in finding the each cell values. 以下代码可以很好地找到每个单元格的值。

With ThisWorkbook
        .Sheets("UpfrontOrder#").Activate

       Set web_table = selenium.findElementByXPath("//table[@id='ctl00_cphContentBody_itemsTable']/tbody")
       Set web_tr = web_table.findElementsByTagName("tr")

       row_count = selenium.findElementsByXPath("//table[@id='ctl00_cphContentBody_itemsTable']/tbody/tr").Count

       For i = 0 To (row_count - 2)

            Set web_td = web_tr(i).findElementsByTagName("td")
            For j = 0 To 6
                   ActiveSheet.Cells((i + 1), (j + 1)).Value = web_td(j).Text
            Next j
        Next i
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM