使用 VBA IE 自动化选择文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13277475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:23:51  来源:igfitidea点击:

using VBA IE automation to select text box

vbams-accessaccess-vba

提问by live2ride

I'm using Access VBA to open Internet Explorer and fill out a form on a website. This form has id of auto number which changes every so often but "_id" as per below is always the same, and I'm not sure if or how I can use the "_id" to insert the value into that field

我正在使用 Access VBA 打开 Internet Explorer 并在网站上填写表单。此表单具有自动编号的 id,它经常更改,但下面的“_id”始终相同,我不确定是否或如何使用“_id”将值插入该字段

this is what i have so far, thanks Tim im not sure how exactly this site works as im fairly new here but this is what i have so far, one last question on Radio Buttons

这是我到目前为止所拥有的,谢谢蒂姆,我不确定这个网站究竟是如何运作的,因为我在这里还很新,但这是我到目前为止所拥有的,关于单选按钮的最后一个问题

Dim ie As InternetExplorer
Dim url As String
Dim htmlDoc As MSHTML.HTMLDocument ' html object lib
Dim htmlInputElemen As MSHTML.HTMLInputElement
Dim htmlElementCol As MSHTML.IHTMLElementCollection

url = www.abc.com
ie.navigate url
‘ this is for text box
Set htmlDoc = ie.Document
Set htmlElementCol = htmlDoc.getElementsByTagName("INPUT")
    For Each htmlInputElemen In htmlElementCol
        If htmlInputElemen.getAttribute("title") = "myTitle" Then
            htmlInputElemen.Value =  “myTitle”
'       ElseIf htmlInputElemen.getAttribute("title") = "myTitle2" Then
            htmlInputElemen.Value = “myTitle2”
        End If
   Next htmlInputElemen
‘ this is for combo box
Set htmlElementCol = htmlDoc.getElementsByTagName("select")
Dim htmlSelectElem As MSHTML.HTMLSelectElement
    For Each htmlSelectElem In htmlElementCol
        If htmlSelectElem.getAttribute("title") = "myComboBox" Then
            htmlSelectElem.Value = “myComboBoxValue”
        End If
    Next htmlSelectElem

‘now I have radio buttons which im not sure how to click using title, any ideas?
I tried this but no luck
Set htmlElementCol = htmlDoc.getElementsByTagName("span")
Dim htmlSpanElem As MSHTML.HTMLSpanElement
    For Each htmlSpanElem In htmlElementCol
        If htmlSpanElem.getAttribute("title") =  “ABC” Then
            htmlSpanElem.Click
        End If
    Next htmlSpanElem

<span class="ms-RadioText" title="ABC">
<input id="234sd87s89df">
<label for="234sd87s89df ">ABC</label>

采纳答案by Tim Williams

Dim inputs, el

Set inputs = document.getElementsByTagName("input")

For Each el In inputs
    If el.getAttribute("_id") = "R_Destination" Then
        el.Value = "myValue"
        Exit For
    End If
Next el

EDIT: here's the same code refactored as a function

编辑:这是重构为函数的相同代码

Function GetElement(doc as object, tagName As String, attrName As String, _
                                      attrValue As String) As Object
    Dim rv As Object
    Dim elements, element

    Set inputs = doc.getElementsByTagName(tagName)

    For Each element In elements
        If element.getAttribute(attrName) = attrValue Then
            Set rv = element
            Exit For
        End If
    Next element

    Set GetElement = rv

End Function

Usage:

用法:

Dim el
Set el = GetElement(document, "input", "_id", "R_Destination")
if not el is nothing then el.value = "myValue"