我需要使用 VBA 查找并按下网页上的按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17179872/
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
I need to find and press a button on a webpage using VBA
提问by Michaeljwjr
I have written a macro that will open a webpage, find the spots I need to put the data into, and then I want the macro to hit a prefill button, then hit Ok.
我写了一个宏,可以打开一个网页,找到我需要将数据放入的位置,然后我希望宏点击预填充按钮,然后点击确定。
The page source code for the button is:
该按钮的页面源代码为:
<input type="text" size="30" maxlength="40" name="name" id="name">
<input type="button" value="Prefill" onclick="prefill()">
I've been searching for answers all week and I think I have a basic understanding of how this is supposed to work by running a loop to search for it, but I'm having no luck in my endeavor of actually getting this to work.
我整个星期都在寻找答案,我想我对它应该如何通过运行循环来搜索它应该如何工作有一个基本的了解,但是我在努力让它真正起作用时没有运气。
Can someone show me the loop that will search my page for this button?
有人可以向我展示将在我的页面中搜索此按钮的循环吗?
Thank you in advance.
先感谢您。
Requested code so far
到目前为止请求的代码
Private Sub Populate_Click()
Dim i As Long
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "website" 'make sure you've logged into the page
Do
DoEvents
Loop Until IE.READYSTATE = 3
Do
DoEvents
Loop Until IE.READYSTATE = 4
ActiveSheet.EnableCalculation = False
ActiveSheet.EnableCalculation = True
Call IE.document.getelementbyid("name").SetAttribute("value", ActiveSheet.Range("b2").Value)
Call IE.document.getelementbyid("aw_login").SetAttribute("value", ActiveSheet.Range("a2").Value)
Set objCollection = IE.document.getElementsByTagName("input")
i = 0
While i < objCollection.Length
If objCollection(i).Type = "button" And _
objCollection(i).Name = "Prefill" Then
Set objElement = objCollection(i)
End If
i = i + 1
Wend
objElement.Click
End Sub
采纳答案by Rick
Looks like you are pretty close, this is what I have used to do something similiar
看起来你很接近,这是我曾经做过类似的事情
With ie.document
Set elems = .getelementsbytagname("input")
For Each e In elems
If (e.getattribute("className") = "saveComment") Then
e.Click
Exit For
End If
Next e
End With
You will probably just have to change the if statement.
您可能只需要更改 if 语句。
I also notice that your code refers to .Name = "Prefill"
but your html snippet refers to .value = "Prefill"
我还注意到您的代码指的是.Name = "Prefill"
但您的 html 片段指的是.value = "Prefill"