我是VBA的初学者,但我未能在我的Excel电子表格的网络组合框或列表框中自动选择国家/地区名称。我的代码只输入国家/地区名称,但不选择它。 如何更改此代码,以便从Excel电子表格中选择国家/地区名称,并在Web组合框中选择相同的循环。护照号码,DOB和国籍在我的代码中是正确的。如果您手动使用,那么您可以在我的电子表格中找到我需要捕获的工作许可证编号。 Chrome Inspect Element屏幕截图随附于此。
我的代码如下:
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
答案 0 :(得分:1)
您寻找的解决方案有点难以提供。从下拉列表中选择NATIONALITY
几乎没有什么棘手的部分。我在脚本中使用了.querySelector()
来简化它。但是,无论您想从下拉菜单中选择哪个国家/地区,它都应该符合您的目的。试一试:
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
当你执行上面的脚本时,它应该会给你想要的结果。
另一种方法是使用比{1}更快的xmlhttp
请求。您需要通过“POST”请求将query string parameter
参数作为字典传递。如果您要更改参数,birth date
,passport
或nationality
只需在QueryString
中执行此操作即可。顺便说一下,Nationality
参数应该填入value
而不是name
,100
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
这就是你的脚本应该是这样的:< / p>
Microsoft XML, V6.0
参考添加到库:
NAME
运行上述脚本,您应该获得所需搜索的ID
和<additionalClasspathElements>
<additionalClasspathElement>${user.home}/.m2/repository/path/to/jar/file</additionalClasspathElement>
</additionalClasspathElements>
。