我正在尝试自动登录网站:
https://www.check-mot.service.gov.uk/
但是输入文本框的ID会随机变化,有没有办法扫描代码并建立它的当前ID?
我尝试使用GetElementsByTagName
,但这不起作用。
当我使用inspect元素时,它将我带到这一行:
202413237510 随机更改以下代码行。
<input name="202413237510" class="form-control" id="202413237510" type="text" value="">
下面的代码:
<form name="moth-search" id="EVL" action="/" method="POST">
<fieldset>
<legend class="form-title heading-medium visuallyhidden">Enter the vehicle registration</legend>
<input name="_csrf_token" type="hidden" value="7A9313B6-6834-04E2-56BE-D6966FFE041F">
<div class="form-group apiary-22453 ddt">
<label class="form-label form-label-bold" for="1700806253">
<span>Do not fill this field</span>
<span class="form-hint">For example, CU57ABC</span>
</label>
<input name="1700806253" tabindex="-1" class="form-control" id="1700806253" type="text" value="">
</div>
<div class="form-group is-on-show tyu-33">
<label class="form-label form-label-bold" for="reg-number">
<span>Do not enter anything in this field</span>
<span class="form-hint">For example, CU57ABC</span>
</label>
<input name="reg-number" tabindex="-1" class="form-control" id="reg-number" type="text" value="">
</div>
<div class="form-group hoth-field it-290">
<label class="form-label form-label-bold" for="202413237510">
<span><span class="sr-only">Enter your</span> Registration number (number plate) <span class="sr-only">into this field only</span></span>
<span class="form-hint">For example, CU57ABC</span>
</label>
<input name="202413237510" class="form-control" id="202413237510" type="text" value="">
</div>
<div class="form-group it-290 salad-box">
<label class="form-label form-label-bold" for="vehicle-manufacturer">
<span>This field should be left empty</span>
<span class="form-hint">For example, CU57ABC</span>
</label>
<input name="vehicle-manufacturer" tabindex="-1" class="form-control" id="vehicle-manufacturer" type="text" value="">
</div>
<div class="form-group keep-hidden isOnshow">
<label class="form-label form-label-bold" for="registration">
<span>Do not fill this field</span>
<span class="form-hint">For example, CU57ABC</span>
</label>
<input name="registration" tabindex="-1" class="form-control" id="registration" type="text" value="">
</div>
<div class="form-group bee-hive tyu-33">
<label class="form-label form-label-bold" for="registration-number">
<span>Do not fill this field</span>
<span class="form-hint">For example, CU57ABC</span>
</label>
<input name="registration-number" tabindex="-1" class="form-control" id="registration-number" type="text" value="">
</div>
答案 0 :(得分:1)
您没有显示任何代码,因此我不确定您是如何获取该文档的。我的示例使用来自MS Internet Controls的IE对象,然后使用VBScript正则表达式中的RegExp对象。
您应该能够提取RegExp常量,变量和代码,以从文档正文变量中获取您的ID。
此正则表达式搜索由其名称中的数字组成的匹配输入元素 - 但诀窍是忽略定义中具有tabindex="-1"
的那些元素。
Sub ExtractID()
'To use this example you'll need two references
'Open the VBA editor and pull down Tools | References menu
'- select "Microsoft Internet Controls"
'- select "Microsoft VBScript Regular Expressions 5.5"
Const FIND_NEW_ID As String = "name=""(\d)*"" class=""form-control"" id=""(\d)*"""
Dim ie As SHDocVw.InternetExplorer
Dim regEx As New RegExp
Dim idMatches As MatchCollection
Dim idMatch As Match
Dim strHTML As String
Dim strID As String
Set ie = New SHDocVw.InternetExplorer
With ie
.Navigate "https://www.check-mot.service.gov.uk/"
Do While .ReadyState <> READYSTATE_COMPLETE Or .Busy = True
DoEvents
Loop
strHTML = ie.Document.body.innerHTML
'Set doc = .Document
'strHTML = doc.body.innerHTML
With regEx
.Global = True
.Multiline = True
.IgnoreCase = False
.Pattern = FIND_NEW_ID
End With
If regEx.Test(strHTML) Then
Set idMatches = regEx.Execute(strHTML)
If idMatches.Count = 1 Then
strID = Mid$(idMatches(0).Value, 7) ' remove name from front
strID = Left$(strID, InStr(strID, """") - 1) ' pull ID from double quotes
MsgBox "Found ID: " & strID
Else
MsgBox "Not Going to Work - we found multiples"
End If
Else
MsgBox ("No ID Found")
End If
End With
End Sub
Regex101.com是一个针对HTML文档测试正则表达式的绝佳网站