英文:
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
以下是我尝试的输出:
请以正确的代码指导我。非常感谢。
英文:
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 -->
<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>
<!-- 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 "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
Below is the output that im trying for
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 "your url"
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论