【问题标题】:getElementById where the id changes randomlyid 随机变化的 getElementById
【发布时间】:2018-05-18 22:59:54
【问题描述】:

我正在尝试自动登录:

https://www.check-mot.service.gov.uk/

但是输入文本框的ID是随机变化的,有没有办法扫描代码并确定它的当前ID?

我尝试过使用GetElementsByTagName,但没有奏效。

当我使用检查元素时,它会将我带到这一行:

下面这行代码随机变化的是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>

【问题讨论】:

  • 您可以添加您现在使用的 VBA 来解析文档 html 吗?我可能会向您展示正则表达式 name="(\d)*" class="form-control" id="(\d)*" 表达式以及如何提取 id
  • 要获取当前 ID,您可以尝试 Set post = HTML.querySelector("input.form-control"): MsgBox post.getAttribute("id") 这里 HTMLHTMLDocumentpostObject。如果我能理解您的要求。

标签: ms-access internet-explorer vba getelementbyid


【解决方案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 文档测试正则表达式

【讨论】:

  • 感谢 dbmitch,不熟悉 vbscript,因此将不得不去尝试并准确地测试您的代码在做什么,它可以工作,但不喜欢不知道如何做。
  • 很高兴它对您有用。检查 VBA cmets 的代码。至于正则表达式,这可能是一个终身学习经验,但你的非常简单 - 它只是查找该输入行并通过将 idname 限制为非特定用引号括起来的任意数量的数字(\d)*
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-26
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多