id 随机变化的 getElementById

getElementById where the id changes randomly

我正在尝试自动登录以下网站:

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>

您没有显示任何代码,所以我不确定您是如何获得文档的。我的示例使用 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 文档

测试正则表达式