网页上的复选框使用 Excel VBA
Checkbox on web page using Excel VBA
我正在尝试使用 Excel VBA 检查网页上的复选框。我的代码不起作用。 (没有报错但是打勾了)
如何在网页上显示勾号?
我在网络抓取方面做错了吗?
HTML:
<div class="slick-header ui-state-default">
<div class="slick-header-columns ui-sortable" style="left: -1000px;" unselectable="on">
<div title="Sélectionner / désélectionner tout" class="ui-state-default slick-header-column" id="slickgrid_951473vssGrid_rowSelect_123527" style="width: 21px;">
<div class="header">
<input type="checkbox">
</div>
</div>
<div class="ui-state-default slick-header-column" id="slickgrid_951473purchase_order" style="width: 104px;">
<div class="header slick-sort-header"><span class="slick-column-name">Bon de commande</span></div><span class="slick-sort-indicator"></span>
<div class="slick-resizable-handle"></div>
</div>
<div class="ui-state-default slick-header-column" id="slickgrid_951473purchase_order_date" style="width: 104px;">
<div class="header slick-sort-header"><span class="slick-column-name">Date de la commande</span></div><span class="slick-sort-indicator slick-sort-indicator-asc"></span>
<div class="slick-resizable-handle"></div>
</div>
<div class="ui-state-default slick-header-column" id="slickgrid_951473latest_ship_date" style="width: 103px;">
<div class="header slick-sort-header"><span class="slick-column-name">Date d'expédition maximale</span></div><span class="slick-sort-indicator"></span>
<div class="slick-resizable-handle"></div>
</div>
<div class="ui-state-default slick-header-column" id="slickgrid_951473vendor_code" style="width: 103px;">
<div class="header slick-sort-header"><span class="slick-column-name">Vendor code</span></div><span class="slick-sort-indicator"></span>
<div class="slick-resizable-handle"></div>
</div>
<div class="ui-state-default slick-header-column" id="slickgrid_951473payee_code" style="width: 103px;">
<div class="header slick-sort-header"><span class="slick-column-name">Code bénéficiaire</span></div><span class="slick-sort-indicator"></span>
<div class="slick-resizable-handle"></div>
</div>
<div class="ui-state-default slick-header-column" id="slickgrid_951473warehouse_id" style="width: 103px;">
<div class="header slick-sort-header"><span class="slick-column-name">Entrepôt de destination</span></div><span class="slick-sort-indicator"></span>
<div class="slick-resizable-handle"></div>
</div>
<div class="ui-state-default slick-header-column" id="slickgrid_951473currency_code" style="width: 103px;">
<div class="header slick-sort-header"><span class="slick-column-name">Devise</span></div><span class="slick-sort-indicator"></span>
<div class="slick-resizable-handle"></div>
</div>
<div class="ui-state-default slick-header-column" id="slickgrid_951473status" style="width: 103px;">
<div class="header slick-sort-header"><span class="slick-column-name">Statut</span></div><span class="slick-sort-indicator"></span></div>
</div>
</div>
VBA代码:
Sub automationAmazon()
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
With ie
.Visible = True
.navigate "link"
Do While .busy
DoEvents
Loop
Do While .readyState <> 4
DoEvents
Loop
End With
Set Email = ie.document.getelementbyid("ap_email")
Email.Value = "email@exemple.com"
Set Password = ie.document.getelementbyid("ap_password")
Password.Value = "xxx"
Set submit = ie.document.getelementbyid("signInSubmit")
submit.Click
With ie
.navigate "link"
Do While .busy
DoEvents
Loop
Do While .readyState <> 4
DoEvents
Loop
End With
ie.document.all("searchBy").Value = "PURCHASE_ORDER"
ie.document.all("purchaseOrderNumber").Value = ThisWorkbook.Sheets("Data").Cells(1, 2).Text
ie.document.getelementbyid("create-invoice-search-submit").Click
Dim cBox As Object
Set cBox = ie.document.getElementsByClassName("header")(0)
cBox.Click
End Sub
HTML:
您要查找的元素位于表单内的 iframe 内。你必须和他们谈判。我已经从文件中读取了您的 html。
您需要的位是:
ie.document.getElementsByTagName("iframe")(0).document.getElementsByTagName("input")(4).Click
表单和 iframe:
我的代码:
Option Explicit
Public Sub test()
Dim oFSO As Object, oFS As Object, sText As String
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFS = oFSO.OpenTextFile("C:\Users\User\Desktop\doc.html")
Do Until oFS.AtEndOfStream
sText = oFS.ReadAll()
Loop
Dim html As New HTMLDocument, b As Object
html.body.innerHTML = sText
Set b = html.getElementsByTagName("iframe")(0).document.getElementsByTagName("input")(4)
Debug.Print b.outerHTML
b.Click
End Sub
正在验证正确的元素:
所选元素外层window的打印输出HTML表明选择了正确的元素:
确保每次点击后加载页面
使用
While IE.Busy Or IE.readyState < 4: DoEvents: Wend
在每次单击事件后允许加载页面。这包括在尝试此点击之前您在登录序列中进行的点击之后。
正在读取 HTML 改编自 ozknows 的文件脚本。
我正在尝试使用 Excel VBA 检查网页上的复选框。我的代码不起作用。 (没有报错但是打勾了)
如何在网页上显示勾号?
我在网络抓取方面做错了吗?
HTML:
<div class="slick-header ui-state-default">
<div class="slick-header-columns ui-sortable" style="left: -1000px;" unselectable="on">
<div title="Sélectionner / désélectionner tout" class="ui-state-default slick-header-column" id="slickgrid_951473vssGrid_rowSelect_123527" style="width: 21px;">
<div class="header">
<input type="checkbox">
</div>
</div>
<div class="ui-state-default slick-header-column" id="slickgrid_951473purchase_order" style="width: 104px;">
<div class="header slick-sort-header"><span class="slick-column-name">Bon de commande</span></div><span class="slick-sort-indicator"></span>
<div class="slick-resizable-handle"></div>
</div>
<div class="ui-state-default slick-header-column" id="slickgrid_951473purchase_order_date" style="width: 104px;">
<div class="header slick-sort-header"><span class="slick-column-name">Date de la commande</span></div><span class="slick-sort-indicator slick-sort-indicator-asc"></span>
<div class="slick-resizable-handle"></div>
</div>
<div class="ui-state-default slick-header-column" id="slickgrid_951473latest_ship_date" style="width: 103px;">
<div class="header slick-sort-header"><span class="slick-column-name">Date d'expédition maximale</span></div><span class="slick-sort-indicator"></span>
<div class="slick-resizable-handle"></div>
</div>
<div class="ui-state-default slick-header-column" id="slickgrid_951473vendor_code" style="width: 103px;">
<div class="header slick-sort-header"><span class="slick-column-name">Vendor code</span></div><span class="slick-sort-indicator"></span>
<div class="slick-resizable-handle"></div>
</div>
<div class="ui-state-default slick-header-column" id="slickgrid_951473payee_code" style="width: 103px;">
<div class="header slick-sort-header"><span class="slick-column-name">Code bénéficiaire</span></div><span class="slick-sort-indicator"></span>
<div class="slick-resizable-handle"></div>
</div>
<div class="ui-state-default slick-header-column" id="slickgrid_951473warehouse_id" style="width: 103px;">
<div class="header slick-sort-header"><span class="slick-column-name">Entrepôt de destination</span></div><span class="slick-sort-indicator"></span>
<div class="slick-resizable-handle"></div>
</div>
<div class="ui-state-default slick-header-column" id="slickgrid_951473currency_code" style="width: 103px;">
<div class="header slick-sort-header"><span class="slick-column-name">Devise</span></div><span class="slick-sort-indicator"></span>
<div class="slick-resizable-handle"></div>
</div>
<div class="ui-state-default slick-header-column" id="slickgrid_951473status" style="width: 103px;">
<div class="header slick-sort-header"><span class="slick-column-name">Statut</span></div><span class="slick-sort-indicator"></span></div>
</div>
</div>
VBA代码:
Sub automationAmazon()
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
With ie
.Visible = True
.navigate "link"
Do While .busy
DoEvents
Loop
Do While .readyState <> 4
DoEvents
Loop
End With
Set Email = ie.document.getelementbyid("ap_email")
Email.Value = "email@exemple.com"
Set Password = ie.document.getelementbyid("ap_password")
Password.Value = "xxx"
Set submit = ie.document.getelementbyid("signInSubmit")
submit.Click
With ie
.navigate "link"
Do While .busy
DoEvents
Loop
Do While .readyState <> 4
DoEvents
Loop
End With
ie.document.all("searchBy").Value = "PURCHASE_ORDER"
ie.document.all("purchaseOrderNumber").Value = ThisWorkbook.Sheets("Data").Cells(1, 2).Text
ie.document.getelementbyid("create-invoice-search-submit").Click
Dim cBox As Object
Set cBox = ie.document.getElementsByClassName("header")(0)
cBox.Click
End Sub
HTML:
您要查找的元素位于表单内的 iframe 内。你必须和他们谈判。我已经从文件中读取了您的 html。
您需要的位是:
ie.document.getElementsByTagName("iframe")(0).document.getElementsByTagName("input")(4).Click
表单和 iframe:
我的代码:
Option Explicit
Public Sub test()
Dim oFSO As Object, oFS As Object, sText As String
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFS = oFSO.OpenTextFile("C:\Users\User\Desktop\doc.html")
Do Until oFS.AtEndOfStream
sText = oFS.ReadAll()
Loop
Dim html As New HTMLDocument, b As Object
html.body.innerHTML = sText
Set b = html.getElementsByTagName("iframe")(0).document.getElementsByTagName("input")(4)
Debug.Print b.outerHTML
b.Click
End Sub
正在验证正确的元素:
所选元素外层window的打印输出HTML表明选择了正确的元素:
确保每次点击后加载页面
使用
While IE.Busy Or IE.readyState < 4: DoEvents: Wend
在每次单击事件后允许加载页面。这包括在尝试此点击之前您在登录序列中进行的点击之后。
正在读取 HTML 改编自 ozknows 的文件脚本。