我需要使用VBA查找并按下网页上的按钮 [英] I need to find and press a button on a webpage using VBA

查看:278
本文介绍了我需要使用VBA查找并按下网页上的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个宏,它会打开一个网页,找到我需要把数据放入的位置,然后我想让宏命中一个预填充按钮,然后点击Ok。



该按钮的页面源代码是:

  input type =textsize =30maxlength =40name =nameid =name> 
< input type =buttonvalue =Prefillonclick =prefill()>

我一直在寻找答案,我想我有一个基本的了解这是如何应该通过运行一个循环来搜索它,但是我没有真正地努力实现这个工作的运气。



有人可以向我显示将搜索我的页面的循环吗?



提前谢谢。



请求代码到目前为止

  Private Sub Populate_Click()
Dim i As Long
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object

设置IE = CreateObject(InternetExplorer.Application)

IE.Visible = True
IE.Navigatewebsite'确保你已经登录页面

Do
DoEvents
循环直到IE.READYSTATE = 3

Do
DoEvents
循环直到IE.READYSTATE = 4
ActiveSheet.EnableCalculation = False
ActiveSheet.EnableCalculation = True
调用IE.document.getelementbyid(name)SetAttribute(value,ActiveSheet.Range(b2)。值)
调用IE.document.getelementbyid(aw_login)SetAttribute (value,ActiveSheet.Range(a2)。值)

设置objCollection = IE.document.getElementsByTagName(input)
i = 0
虽然i< ; objCollection.Length
如果objCollection(i).Type =button和_
objCollection(i).Name =Prefill然后
设置objElement = objCollection(i)
结束如果
i = i + 1
Wend
objElement.Click
End Sub


解决方案

看起来你很亲密,这是我以前做过类似的事情。

 使用ie.document 

设置elems = .getelementsbytagname(input)
对于每个e在elems
如果(e.getattribute( class =saveComment)然后
e.Click
退出
结束如果
下一步e

结束

您可能只需更改if语句。



I还要注意你的代码是指 .Name =Prefill,但你的html片段是指 .value = 预填


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

解决方案

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.

I also notice that your code refers to .Name = "Prefill" but your html snippet refers to .value = "Prefill"

这篇关于我需要使用VBA查找并按下网页上的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆