Selenium VBA:获取具有<tr和<td下的<div class元素的表格内容

huangapple go评论99阅读模式
英文:

Selenium VBA: Get the table content which are having <div class elements under <tr and <td

问题

我想从以下HTML代码中提取表格内容,这些内容在<div class下列出。我尝试了不同的代码,但找不到正确的解决方案。

<table class="ISLogIn AvgTotal" id="invoiceAmountAvgTotalNewDesign" width="100%">
    <tbody><tr>
        <td colspan="8">
            <div class="container invoice-lines-container">
                <div class="list invoice-line-total">
                    <div class="col-xs-4 invoice-line-name">Avg Total</div>
                    <div class="col-xs-6 invoice-line-name"></div>
                    <div class="col-xs-2 invoice-line-value">
                        <strong data-bind="autoNumeric: hasDiscrepancyTotalInvoiceWithDb() ? invoiceAmountDbData().SubTotal : subTotal, autoNumericOptions: $root.moneyNegativeFormat()">135.43</strong>
                    </div>
                </div>
            </div>
        </td>
    </tr>
    <tr>
        <td colspan="8">
            <div class="container invoice-lines-container">
                <div class="list invoice-line-header">
                    <div class="invoice-line-name col-xs-4" data-bind="css: { 'col-xs-3': isEditableAdditionalApprovalCode(), 'col-xs-4': isEditableAdditionalApprovalCode() === false }">
                            <span>Sale </span>
                    </div>
                    <div class="invoice-line-name col-xs-3" data-bind="css: { 'col-xs-2': isEditableAdditionalApprovalCode(), 'col-xs-3': isEditableAdditionalApprovalCode() === false }">
                    </div>
                    <div class="invoice-line-value col-xs-4" data-bind="css: { 'col-xs-6': isEditableAdditionalApprovalCode(), 'col-xs-4': isEditableAdditionalApprovalCode() === false }"></div>
                    <div class="col-xs-1 invoice-line-value">Amount</div>
                </div>
                <div class="list invoice-line-item-total" data-bind="visible: isInternational()" style="display: none;">
                    <div class="col-xs-4 invoice-line-name">Total Sale</div>
                    <div class="col-xs-6 invoice-line-name"></div>
                    <div class="col-xs-2 invoice-line-value">
                    </div>
                </div>
                <div class="list invoice-line-item-total" data-bind="visible: !isInternational()">
                    <div class="col-xs-4 invoice-line-name">Total Sale</div>
                    <div class="col-xs-6 invoice-line-name"></div>
                    <div class="col-xs-2 invoice-line-value">
                        <span data-bind="autoNumeric: hasDiscrepancyTotalInvoiceWithDb() ? invoiceAmountDbData().TotalAmountTax : printTaxAmount(), autoNumericOptions: $root.moneyNegativeFormat()">3.14</span>
                    </div>
                </div>
            </div>
        </td>
    </tr>
    <tr>
        <td colspan="8">
            <div class="container invoice-lines-container">
                <div class="list invoice-line-total">
                    <div class="col-xs-4 invoice-line-name">Total (<span data-bind="text: workInfo.currency">INR</span>)</div>
                    <div class="col-xs-6 invoice-line-name"></div>
                    <div class="col-xs-2 invoice-line-value">
                        <span data-bind="autoNumeric: calculatedTotal(), autoNumericOptions: $root.moneyNegativeFormat()">138.57</span>
                    </div>
                </div>
            </div>
        </td>
    </tr>
</tbody></table>

以下是我尝试提取表格内容的VBA Selenium代码,以表格格式提取为目标。我使用Chrome和Selenium。

Option Explicit
Sub table()
Dim d As New ChromeDriver
Dim iM As Object, iMs As Object
Dim S2 As Long, i As Long
d.Get "My URL"
Application.Wait (Now + TimeValue("00:00:03"))
With Sh2
Set iMs = d.FindElementsByCss(".ISLogIn AvgTotal div[class^='list invoice-line']")
.Activate
For Each iM In iMs
S2 = .Cells(Rows.Count, 2).End(xlUp).Row + 1: .Cells(S2, 1).Select
.Cells(S2, 2) = iM.Text
Next iM
End With
Set iM = Nothing:   Set wOrder = Nothing: Set Sc = Nothing
End Sub

以下是我尝试的输出:

Selenium VBA:获取具有<tr和<td下的<div class元素的表格内容

Selenium VBA:获取具有<tr和<td下的<div class元素的表格内容

请以正确的代码指导我。非常感谢。

英文:

I want to extract the table content from the following HTML code which are listed under <div class. I tried with different codes but i couldn't find the correct solution.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;table class=&quot;ISLogIn AvgTotal&quot; id=&quot;invoiceAmountAvgTotalNewDesign&quot; width=&quot;100%&quot;&gt;
&lt;tbody&gt;&lt;tr&gt;
&lt;td colspan=&quot;8&quot;&gt;
&lt;div class=&quot;container invoice-lines-container&quot;&gt;
&lt;div class=&quot;list invoice-line-total&quot;&gt;
&lt;div class=&quot;col-xs-4 invoice-line-name&quot;&gt;Avg Total&lt;/div&gt;
&lt;div class=&quot;col-xs-6 invoice-line-name&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;col-xs-2 invoice-line-value&quot;&gt;
&lt;strong data-bind=&quot;autoNumeric: hasDiscrepancyTotalInvoiceWithDb() ? invoiceAmountDbData().SubTotal : subTotal, autoNumericOptions: $root.moneyNegativeFormat()&quot;&gt;135.43&lt;/strong&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan=&quot;8&quot;&gt;
&lt;div class=&quot;container invoice-lines-container&quot;&gt;
&lt;div class=&quot;list invoice-line-header&quot;&gt;
&lt;div class=&quot;invoice-line-name col-xs-4&quot; data-bind=&quot;css: { &#39;col-xs-3&#39;: isEditableAdditionalApprovalCode(), &#39;col-xs-4&#39;: isEditableAdditionalApprovalCode() === false }&quot;&gt;
&lt;span&gt;Sale &lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;invoice-line-name col-xs-3&quot; data-bind=&quot;css: { &#39;col-xs-2&#39;: isEditableAdditionalApprovalCode(), &#39;col-xs-3&#39;: isEditableAdditionalApprovalCode() === false }&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;invoice-line-value col-xs-4&quot; data-bind=&quot;css: { &#39;col-xs-6&#39;: isEditableAdditionalApprovalCode(), &#39;col-xs-4&#39;: isEditableAdditionalApprovalCode() === false }&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;col-xs-1 invoice-line-value&quot;&gt;Amount&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;list invoice-line-item-total&quot; data-bind=&quot;visible: isInternational()&quot; style=&quot;display: none;&quot;&gt;
&lt;div class=&quot;col-xs-4 invoice-line-name&quot;&gt;Total Sale&lt;/div&gt;
&lt;div class=&quot;col-xs-6 invoice-line-name&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;col-xs-2 invoice-line-value&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;list invoice-line-item-total&quot; data-bind=&quot;visible: !isInternational()&quot;&gt;
&lt;div class=&quot;col-xs-4 invoice-line-name&quot;&gt;Total Sale&lt;/div&gt;
&lt;div class=&quot;col-xs-6 invoice-line-name&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;col-xs-2 invoice-line-value&quot;&gt;
&lt;span data-bind=&quot;autoNumeric: hasDiscrepancyTotalInvoiceWithDb() ? invoiceAmountDbData().TotalAmountTax : printTaxAmount(), autoNumericOptions: $root.moneyNegativeFormat()&quot;&gt;3.14&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan=&quot;8&quot;&gt;
&lt;div class=&quot;container invoice-lines-container&quot;&gt;
&lt;div class=&quot;list invoice-line-total&quot;&gt;
&lt;div class=&quot;col-xs-4 invoice-line-name&quot;&gt;Total (&lt;span data-bind=&quot;text: workInfo.currency&quot;&gt;INR&lt;/span&gt;)&lt;/div&gt;
&lt;div class=&quot;col-xs-6 invoice-line-name&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;col-xs-2 invoice-line-value&quot;&gt;
&lt;span data-bind=&quot;autoNumeric: calculatedTotal(), autoNumericOptions: $root.moneyNegativeFormat()&quot;&gt;138.57&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;

<!-- end snippet -->

Below is my vba selenium code which grabs all the row information into one cell.
Im trying to extract as table format. I use chrome with selenium.

Option Explicit
Sub table()
Dim d As New ChromeDriver
Dim iM As Object, iMs As Object
Dim S2 As Long, i As Long
d.Get &quot;My URL&quot;
Application.Wait (Now + TimeValue(&quot;00:00:03&quot;))
With Sh2
Set iMs = d.FindElementsByCss(&quot;.ISLogIn AvgTotal div[class^=&#39;list invoice-line&#39;]&quot;)
.Activate
For Each iM In iMs
S2 = .Cells(Rows.Count, 2).End(xlUp).Row + 1: .Cells(S2, 1).Select
.Cells(S2, 2) = iM.Text
Next iM
End With
Set iM = Nothing:   Set wOrder = Nothing: Set Sc = Nothing
End Sub

Below is the output that im trying for

Selenium VBA:获取具有<tr和<td下的<div class元素的表格内容

Selenium VBA:获取具有<tr和<td下的<div class元素的表格内容

Please guide me with the correct code. Thanks a lot.

答案1

得分: 1

这将使您更接近解决方案的代码部分:

    Option Explicit
Sub table()
Dim k As Integer
Dim d As New ChromeDriver
Dim iM As WebElement, iMs As Object
Dim sT As String, iV As Selenium.List
Dim S2 As Long, i As Long
d.Get "您的网址"
Application.Wait (Now + TimeValue("00:00:03"))
With Sh2
Set iMs = d.FindElementsByCss("div[class^='list invoice-line']")
.Activate
For Each iM In iMs
S2 = .Cells(Rows.Count, 2).End(xlUp).Row + 1
sT = getTitle(iM)
Set iV = iM.FindElementsByClass("invoice-line-value")
For k = 1 To iV.Count
If iV(k).Text <> "" Then
.Cells(S2, 3) = iV(k).Text
Exit For
End If
Next
.Cells(S2, 2) = sT
Next iM
End With
Set iM = Nothing: Set wOrder = Nothing: Set Sc = Nothing
Set iV = Nothing
d.Quit
End Sub
Function getTitle(iM As WebElement) As String
Dim iT As WebElement
On Error GoTo notFnd
Set iT = iM.FindElementByClass("col-xs-4")
getTitle = iT.Text
Exit Function
notFnd:
getTitle = ""
End Function

请注意,我将 "your url" 替换为了 "您的网址" 以使其更适应中文环境。

英文:

this will get you closer to your solution

Option Explicit
Sub table()
Dim k As Integer
Dim d As New ChromeDriver
Dim iM As WebElement, iMs As Object
Dim sT As String, iV As Selenium.List
Dim S2 As Long, i As Long
d.Get &quot;your url&quot;
Application.Wait (Now + TimeValue(&quot;00:00:03&quot;))
With Sh2
Set iMs = d.FindElementsByCss(&quot;div[class^=&#39;list invoice-line&#39;]&quot;)
.Activate
For Each iM In iMs
S2 = .Cells(Rows.Count, 2).End(xlUp).Row + 1
sT = getTitle(iM)
Set iV = iM.FindElementsByClass(&quot;invoice-line-value&quot;)
For k = 1 To iV.Count
If iV(k).Text &lt;&gt; &quot;&quot; Then
.Cells(S2, 3) = iV(k).Text
Exit For
End If
Next
.Cells(S2, 2) = sT
Next iM
End With
Set iM = Nothing: Set wOrder = Nothing: Set Sc = Nothing
Set iV = Nothing
d.Quit
End Sub
Function getTitle(iM As WebElement) As String
Dim iT As WebElement
On Error GoTo notFnd
Set iT = iM.FindElementByClass(&quot;col-xs-4&quot;)
getTitle = iT.Text
Exit Function
notFnd:
getTitle = &quot;&quot;
End Function

huangapple
  • 本文由 发表于 2023年8月4日 23:25:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定