IE Web 自动化 - 如何使用 Excel VBA/XML 宏自动从组合框中获取 select 值
IE Web Automation - How to auto select value from combo box using Excel VBA/XML Macro
我是 VBA 的初学者,我未能在 Excel 电子表格的网络组合框或列表框中自动 select 国家/地区名称。我的代码仅输入国家/地区名称,但未输入 select。
我如何更改此代码,以便它可以从我的 Excel 电子表格和 select 中选择国家名称作为循环在网络组合框中相同。我的代码上的护照号码、DOB 和国籍是正确的。如果您将手动使用,那么您可以在我的电子表格中找到我需要捕获的工作许可证号。 Chrome 随附检查元素屏幕截图。
我的代码如下:
Sub MOL()
Dim IE As New SHDocVw.InternetExplorer
Dim Doc As MSHTML.HTMLDocument
Dim Buttons As MSHTML.IHTMLElementCollection
Dim Button As MSHTML.IHTMLElement
Dim HTMLInput As MSHTML.IHTMLElement
Dim Tags As MSHTML.IHTMLElement
Dim HTMLTables As MSHTML.IHTMLElementCollection
Dim HTMLTable As MSHTML.IHTMLElement
Dim HTMLRow As MSHTML.IHTMLElement
Dim HTMLCell As MSHTML.IHTMLElement
Dim Alltext As IHTMLElementCollection
Application.ScreenUpdating = False
'Application.Calculation = xlCalculationManual
'Application.EnableEvents = False
On Error Resume Next
IE.Visible = True
IE.navigate "https://eservices.mol.gov.ae/SmartTasheel/Complain/IndexLogin?lang=en-gb"
Do While IE.readyState <> READYSTATE_COMPLETE: Loop
Set Doc = IE.document
Set Buttons = Doc.getElementsByTagName("Button")
Buttons(2).Click
Do While IE.readyState <> READYSTATE_INTERACTIVE = 3: Loop
Set HTMLInputs = Doc.getElementsByTagName("Input")
HTMLInputs(46).Value = "somevalue"
HTMLInputs(48).Value = "24/02/1990"
HTMLInputs(47).Value = "India"
Buttons(21).Click
End Sub
您寻找的解决方案有点难以提供。从下拉列表中 select 到 NATIONALITY
几乎没有棘手的部分。我在脚本中使用了 .querySelector()
以使其简洁。但是,无论您想从下拉菜单中选择 select 哪个国家/地区,它都应该符合您的目的。试一试:
Sub GetInfo()
Dim IE As New InternetExplorer, HTML As HTMLDocument, post As Object, URL$
URL = "https://eservices.mol.gov.ae/SmartTasheel/Complain/IndexLogin?lang=en-gb"
With IE
.Visible = True
.navigate URL
While .Busy = True Or .readyState < 4: DoEvents: Wend
Set HTML = .document
HTML.getElementById("TransactionInfo_WorkPermitNumber").innerText = "2659558"
HTML.querySelector("button[ng-click='showEmployeeSearch()']").Click
Application.Wait Now + TimeValue("00:00:03") ''If for some reason the script fails, make sure to increase the delay
HTML.getElementById("txtPassportNumber").Value = "J2659558"
HTML.getElementById("Nationality").Focus
For Each post In HTML.getElementsByClassName("ng-scope")
With post.getElementsByClassName("ng-binding")
For I = 0 To .Length - 1
If .item(I).innerText = "INDIA" Then ''you can change the country name here to select from dropdown
.item(I).Click
Exit For
End If
Next I
End With
Next post
HTML.getElementById("txtBirthDate").Value = "24/02/1990"
HTML.querySelector("button[onclick='SearchEmployee()']").Click
End With
End Sub
要添加到库中的引用:
Microsoft Internet Controls
Microsoft HTML Object library
当你执行上面的脚本时,它应该会给你想要的结果。
另一种方法是使用比 IE 快得多的 xmlhttp
请求。您需要通过 "POST" 请求将 query string parameter
参数作为字典传递。如果您想更改 birth date
、passport
或 nationality
中的参数,只需在 QueryString
中进行即可。顺便说一句,Nationality
参数应该用 value
而不是 name
来填充,100
for INDIA.
这就是你的脚本应该是这样的:
Sub Get_Data()
Dim res As Variant, QueryString$, ID$, Name$
QueryString = "{""PersonPassportNumber"":""J2659558"",""PersonNationality"":""100"",""PersonBirthDate"":""24/02/1990""}"
With New XMLHTTP
.Open "POST", "https://eservices.mol.gov.ae/SmartTasheel/Dashboard/GetEmployees", False
.setRequestHeader "User-Agent", "Mozilla/5.0"
.setRequestHeader "Content-Type", "application/json"
.send QueryString
res = .responseText
End With
ID = Split(Split(Split(res, "Employees"":")(1), "ID"":""")(1), """,")(0)
Name = Split(Split(Split(res, "Employees"":")(1), "OtherData2"":""")(1), """}")(0)
[A1] = ID: [B1] = Name
End Sub
要添加到库中的引用:
Microsoft XML, V6.0
运行上面的脚本,你应该得到你需要的搜索的NAME
和ID
。
我是 VBA 的初学者,我未能在 Excel 电子表格的网络组合框或列表框中自动 select 国家/地区名称。我的代码仅输入国家/地区名称,但未输入 select。 我如何更改此代码,以便它可以从我的 Excel 电子表格和 select 中选择国家名称作为循环在网络组合框中相同。我的代码上的护照号码、DOB 和国籍是正确的。如果您将手动使用,那么您可以在我的电子表格中找到我需要捕获的工作许可证号。 Chrome 随附检查元素屏幕截图。
我的代码如下:
Sub MOL()
Dim IE As New SHDocVw.InternetExplorer
Dim Doc As MSHTML.HTMLDocument
Dim Buttons As MSHTML.IHTMLElementCollection
Dim Button As MSHTML.IHTMLElement
Dim HTMLInput As MSHTML.IHTMLElement
Dim Tags As MSHTML.IHTMLElement
Dim HTMLTables As MSHTML.IHTMLElementCollection
Dim HTMLTable As MSHTML.IHTMLElement
Dim HTMLRow As MSHTML.IHTMLElement
Dim HTMLCell As MSHTML.IHTMLElement
Dim Alltext As IHTMLElementCollection
Application.ScreenUpdating = False
'Application.Calculation = xlCalculationManual
'Application.EnableEvents = False
On Error Resume Next
IE.Visible = True
IE.navigate "https://eservices.mol.gov.ae/SmartTasheel/Complain/IndexLogin?lang=en-gb"
Do While IE.readyState <> READYSTATE_COMPLETE: Loop
Set Doc = IE.document
Set Buttons = Doc.getElementsByTagName("Button")
Buttons(2).Click
Do While IE.readyState <> READYSTATE_INTERACTIVE = 3: Loop
Set HTMLInputs = Doc.getElementsByTagName("Input")
HTMLInputs(46).Value = "somevalue"
HTMLInputs(48).Value = "24/02/1990"
HTMLInputs(47).Value = "India"
Buttons(21).Click
End Sub
您寻找的解决方案有点难以提供。从下拉列表中 select 到 NATIONALITY
几乎没有棘手的部分。我在脚本中使用了 .querySelector()
以使其简洁。但是,无论您想从下拉菜单中选择 select 哪个国家/地区,它都应该符合您的目的。试一试:
Sub GetInfo()
Dim IE As New InternetExplorer, HTML As HTMLDocument, post As Object, URL$
URL = "https://eservices.mol.gov.ae/SmartTasheel/Complain/IndexLogin?lang=en-gb"
With IE
.Visible = True
.navigate URL
While .Busy = True Or .readyState < 4: DoEvents: Wend
Set HTML = .document
HTML.getElementById("TransactionInfo_WorkPermitNumber").innerText = "2659558"
HTML.querySelector("button[ng-click='showEmployeeSearch()']").Click
Application.Wait Now + TimeValue("00:00:03") ''If for some reason the script fails, make sure to increase the delay
HTML.getElementById("txtPassportNumber").Value = "J2659558"
HTML.getElementById("Nationality").Focus
For Each post In HTML.getElementsByClassName("ng-scope")
With post.getElementsByClassName("ng-binding")
For I = 0 To .Length - 1
If .item(I).innerText = "INDIA" Then ''you can change the country name here to select from dropdown
.item(I).Click
Exit For
End If
Next I
End With
Next post
HTML.getElementById("txtBirthDate").Value = "24/02/1990"
HTML.querySelector("button[onclick='SearchEmployee()']").Click
End With
End Sub
要添加到库中的引用:
Microsoft Internet Controls
Microsoft HTML Object library
当你执行上面的脚本时,它应该会给你想要的结果。
另一种方法是使用比 IE 快得多的 xmlhttp
请求。您需要通过 "POST" 请求将 query string parameter
参数作为字典传递。如果您想更改 birth date
、passport
或 nationality
中的参数,只需在 QueryString
中进行即可。顺便说一句,Nationality
参数应该用 value
而不是 name
来填充,100
for INDIA.
这就是你的脚本应该是这样的:
Sub Get_Data()
Dim res As Variant, QueryString$, ID$, Name$
QueryString = "{""PersonPassportNumber"":""J2659558"",""PersonNationality"":""100"",""PersonBirthDate"":""24/02/1990""}"
With New XMLHTTP
.Open "POST", "https://eservices.mol.gov.ae/SmartTasheel/Dashboard/GetEmployees", False
.setRequestHeader "User-Agent", "Mozilla/5.0"
.setRequestHeader "Content-Type", "application/json"
.send QueryString
res = .responseText
End With
ID = Split(Split(Split(res, "Employees"":")(1), "ID"":""")(1), """,")(0)
Name = Split(Split(Split(res, "Employees"":")(1), "OtherData2"":""")(1), """}")(0)
[A1] = ID: [B1] = Name
End Sub
要添加到库中的引用:
Microsoft XML, V6.0
运行上面的脚本,你应该得到你需要的搜索的NAME
和ID
。