VBA单击IE上的按钮



我经常使用VBA,但是在IE自动化方面,我仍然是初学者。我需要创建一个代码来单击我公司的特定网站上的"按钮"。此按钮打开一个小菜单,我需要选择一些东西(我猜想稍后再处理(。事实是,该按钮没有显示任何ID或名称,这是一个跨度,我遇到了麻烦。下面是当我按F12并通过按钮检查元素时的HTML:

 <span class="search_for_watchers">
 <a href="/watchers/new?project_id=pendencia" data-method="get" data-remote="true">Procurar por outros observadores para adiconar</a>
 </span>

我尝试了很多建议,以至于我通过Internet找到了很多建议。我尝试通过ID,名称,班级名称等获取元素,但没有成功。以下是我正在使用的代码:

Sub Automate_IE_Load_Page()
    Dim i As Long
    Dim URL As String
    Dim ie As Object
    Dim objElement As Object
    Dim objCollection As Object
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    URL = "Examplesite.com"
    ie.Navigate URL
    Application.StatusBar = URL & " is loading. Please wait..."
    Do While ie.READYSTATE = 4: DoEvents: Loop
    Do Until ie.READYSTATE = 4: DoEvents: Loop
    Application.StatusBar = URL & " Loaded"
    botao = ie.Document.getElementsByClassName("search_for_watchers")
    botao.Click
End Sub

它没有显示任何错误..它只是没有打开我想要的子菜单,因此该按钮未单击。

谢谢!

getElementsByClassName是复数的;即它代表一个集合。以下代表带有此类的第一个对象,然后单击其中的第一个锚标记。

ie.Document.getElementsByClassName("search_for_watchers")(0).getElementsByTagName("a")(0).click

如果要将对象var分配到元素,然后使用var单击,则需要设置var。

您在这里输入无尽的循环:

Do While ie.READYSTATE = 4: DoEvents: Loop
Do Until ie.READYSTATE = 4: DoEvents: Loop

,例如,看起来像这样:

Do While ie.READYSTATE = 4
    DoEvents
Loop

但是:代替VBA中的新行。


确保更改循环,因此类似的东西:

Do While NOT ie.READYSTATE = 4: DoEvents: Loop

您也可以通过标签循环,找到要单击的标签,如:

Set tags = ie.document.getElementsByTagName("a")
For Each tagx In tags
    If tagx.innerText = "Procurar por outros observadores para adiconar" Then
        tagx.Click
        Exit For
    End If
Next

最新更新