Html 使用 VBA 代码单击网页上的按钮

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

Use VBA code to click on a button on webpage

htmlvbaclickwebpage

提问by dmr

I am editing a vba program and want to code the vba to click on a button in a webpage. The html for the button is:

我正在编辑一个 vba 程序,并希望对 vba 进行编码以单击网页中的按钮。按钮的 html 是:

<input type="image" src="/lihtml/test_button3.gif" align="left" alt="File_Certificate_Go"/>

I imagine I would have to set a variable to getElementBy??? and then variable.click, but I can't figure out how exactly to get the element (because it doesn't have a name or id, and I can't give it one because it is not my webpage).

我想我必须为 getElementBy 设置一个变量???然后是variable.click,但我不知道如何准确地获取元素(因为它没有名称或ID,我不能给它一个,因为它不是我的网页)。

Any help is greatly appreciated!

任何帮助是极大的赞赏!

采纳答案by Fionnuala

Perhaps something on the lines of:

也许是这样的:

Set tags = wb.Document.GetElementsByTagname("Input")

For Each tagx In tags
    If tagx.alt = "File_Certificate_Go" Then
        tagx.Click
    End If
Next

Where wb is the WebBrowser control.

其中 wb 是 WebBrowser 控件。

回答by John

Is there a reason you couldn't give the element an id?

有什么理由不能给元素一个 id 吗?

i.e.:

IE:

<input id='myButton' type=image src="/lihtml/test_button3.gif" align=left alt=File_Certificate_Go> 

then:

然后:

document.getElementById('myButton').click()

edit: Based on your comment, you'd have to grab all input elements on the page, and then cycle through them looking for the one that makes your input unique:

编辑:根据您的评论,您必须获取页面上的所有输入元素,然后循环浏览它们以查找使您的输入独一无二的元素:

var elms = document.getElementsByTagName("input"); 
for (var i=0; i< elms.length; i++) 
    if(elms[i].src = '/lihtml/test_button3.gif') { elms[i].click(); }

Something along those lines anyway

无论如何