Html 使用vba将文本输入到html输入框中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20611714/
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-08-28 22:30:59  来源:igfitidea点击:

Input text into html input box using vba

htmlvba

提问by user3107346

I am trying to input a value into a from on an internet explorer page using VBA. I have seen similar posts but can't seem to pinpoint my problem. I am somewhat fluent with VBA but HTML is new to me. The ID attributes change on the webpage I am accessing every time you load the page. Here is a shot of the HTML element I am trying to access:

我正在尝试使用 VBA 在 Internet Explorer 页面上输入一个值。我看过类似的帖子,但似乎无法指出我的问题。我对 VBA 有点流利,但 HTML 对我来说是新的。每次加载页面时,我访问的网页上的 ID 属性都会发生变化。这是我尝试访问的 HTML 元素的截图:

<input type="text" size="20" style="text-align: right; width: 261px;" autocomplete="off"    id="ext-comp-1067" name="user_numbers.1.number_1" class="x-form-text x-form-field x-form- num-field" title="">

The code I am currently using is here:

我目前使用的代码在这里:

    Sub OpenWebPage()

    Dim ObjCollection As Object
   Dim i As Long
Dim objElement As Object
Dim doc As Object
Dim form As Object



    Dim ie As Object
    Set ie = CreateObject("INTERNETEXPLORER.APPLICATION")
    ie.Navigate "http://11.182.123.21/#!/view/dashboards/dashboard-1"
    ie.Visible = True
    While ie.Busy
        DoEvents
   Wend


    Application.Wait Now + TimeValue("00:00:10")

 Set ObjCollection = ie.Document.getElementsByName("user_numbers.1.number_1").Value = "123"

End sub

The name "user_number.1.number_1" I think is the only element in the html that I can use to find this input box, the ID changes every time you open the webpage. How do I got about entering a value in this field?

我认为名称“user_number.1.number_1”是 html 中唯一可以用来查找此输入框的元素,每次打开网页时 ID 都会更改。如何在此字段中输入值?

Thank you!

谢谢!

回答by Pradeep Kumar

As sid mentioned, you have an extra "=" sign.

正如 sid 提到的,你有一个额外的“=”符号。

Replace

代替

Set ObjCollection = ie.Document.getElementsByName("user_numbers.1.number_1").Value = "123"

with

ie.Document.getElementsByName("user_numbers.1.number_1")(0).Value = "123"

This is just off my head and not tested code. If this doesn't work replace (0)in the above code with (1).

这只是我的想法,没有经过测试的代码。如果这不起作用(0),请将上面的代码替换为(1).

If there is no other element on the page with the same name then this will work. Otherwise replace (0)in the above statement with appropriate ordinal position.

如果页面上没有其他同名元素,那么这将起作用。否则(0)用适当的序数位置替换上面的语句。

回答by ron

How about finding the ID everytime like this

每次都这样找ID怎么样

my_var = ie.doc.body.innerhtml

pos_1 = instr(1, my_var, "user_numbers.1.number_1", vbTextCompare)
pos_2 = InStrRev(my_var, "id", pos_1, vbTextCompare)
pos_3 = instr(pos_2, my_var, "name", vbTextCompare)

my_id = mid(my_var, 3+pos_2, (-1+pos_3) - (3+pos_2)

now try

现在试试

ie.Document.getElementById(my_id).Value = "123"

I can't get access to your url, so you might have to tweak the numbers added and subtracted in the "mid" function

我无法访问您的网址,因此您可能需要调整“mid”函数中添加和减去的数字