簡體   English   中英

使用Selenium VBA將動態網頁表中的單元格值帶到excel單元格

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

我是Selenium VBA的新手,並且在Google谷歌搜索后創建了以下代碼,以便使用Selenium VBA從動態網頁中獲取每個單元格的值。 我在接收web_tr和web_td Web元素中的Web元素時遇到錯誤

嘗試使用此行:

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

我得到的是標題行項目的第一個值,盡管它位於循環中。

Web_tr和web_td是Web元素,在這里接收到這些變量的Web元素是問題。 請協助。


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

我對VBA版本的Selenium並不是很熟悉,但是據我所知,我認為有一個問題引起了您遇到的錯誤。 當您使用.findElements* (復數)時,它將返回元素的集合,而不僅僅是單個元素。 您的第一行(下面)使用.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****

您可以嘗試執行以下兩種修復方法之一...

  1. .findElements()更改為.findElement()以便僅獲取第一個(並且可能僅?)表。 如果只想要第一個,這是解決此問題的正確方法。

  2. 在第一行的元素集合中添加一個索引,例如change web_table. web_table(1). 如果要使用第一個TABLE標記以外的其他TABLE標記,請添加正確的引用。

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

附加說明:我還有另一個建議來簡化您的某些代碼。 在您的For i循環中,您具有2-7的硬編碼索引,我認為您可以輕松地使用循環。 我更新了下面的代碼。 這完成了兩件事...

  1. 它消除了頁面的重復刮擦。 在原始代碼中,您一次刮取一個元素。 在下面的代碼中,我獲取了TD的集合,然后遍歷它們。 這樣,我只刮了一次頁面即可得到我關心的所有元素,然后處理該集合。 效率更高。
  2. 簡化代碼。
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

謝謝你們倆

以下代碼可以很好地找到每個單元格的值。

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