我写了一个宏,它将打开一个网页,找到我需要放入数据的点,然后我想让宏点击一个预填充按钮,然后点击确定.
该按钮的页面源代码是:
<input type="text" size="30" maxlength="40" name="name" id="name">
<input type="button" value="Prefill" onclick="prefill()">
Run Code Online (Sandbox Code Playgroud)
我整个星期都在寻找答案,我想我已经基本了解了这应该是如何通过运行循环来搜索它,但我没有运气,我的努力实际上让它工作.
有人能告诉我将在我的页面上搜索此按钮的循环吗?
先感谢您.
到目前为止请求的代码
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
Run Code Online (Sandbox Code Playgroud)
看起来你很接近,这就是我曾经做过类似的事情
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
Run Code Online (Sandbox Code Playgroud)
您可能只需要更改 if 语句即可。
我还注意到您的代码引用了.Name = "Prefill"但您的 html 片段引用了.value = "Prefill"