首页 > 解决方案 > IE 点击没有关联链接的按钮(使用 Excel VBA)

问题描述

我想点击网页上的“按钮”,但我的问题是这个按钮似乎没有附加链接。顺便说一句,你可以看到我对网络浏览器的语言一点也不熟悉。无论如何,我最常使用 Internet Explorer,这是我目前拥有的代码

Sub click_button_no_hlink()


Dim i As Long
Dim IE As Object
Dim Doc As Object
Dim objElement As Object
Dim objCollection As Object

Set IE = CreateObject("InternetExplorer.Application")               'create IE instance

IE.Visible = True

IE.Navigate "https://apex.xyz.qc.ca/apex/prd1/f?p=135:LOGIN_DESKTOP::::::"  ' Adress of web page

Do While IE.Busy                                                     'loading page
    Application.Wait DateAdd("s", 1, Now)
Loop

'-------------Usually I would do something like that and it would works well.-----------------
Set link = IE.document.getElementsByTagName("a")
For Each l In link
    a = l.innerText
    If l.innerText = "Créer" Then                    '
        l.Click
        Exit For
    End If
Next
'-------------That method works fine on other type of hlink to the page by the way------------


'-----------------------I also tried various methods around this with no luck------------------------------
Set objCollection = IE.document.getElementsByClassName("rc-content-buttons")                  'recherche du bouton "Créer"
'---------------------------------

'--------------------or this also doesn't work-------------------------------
For Each btn In IE.document.getElementsByClassName("rc-content-buttons")
    If btn.getAttribute("id") = "B91938241817236808" Then
        btn.Click
        Exit For
    End If
Next btn

End Sub 

为了清楚起见,这里是我试图与之交互的“按钮”周围的网页代码。 javascript代码

我做了很多研究,但我现在处于死胡同。任何帮助将不胜感激。
提前发送。

解决的最终代码:在 DOMENIC、QHARR 和 Yu Zhou 的帮助下

Sub click_button_no_hlink()


Dim i As Long
Dim IE As Object
Dim Doc As Object
Dim objElement As Object
Dim objCollection As Object

Set IE = CreateObject("InternetExplorer.Application")               'create IE instance

IE.Visible = True

IE.Navigate "https://apex.xyz.qc.ca/apex/prd1/f?p=135:LOGIN_DESKTOP::::::"  ' Adress of web page

While IE.Busy: DoEvents: Wend             'loading page

IE.document.querySelector("[value='Créer']").FireEvent "onclick"   'works like a charm

            '  IE.document.querySelector("div.rc-content-buttons input").Click         
                     'also works but speaks less by itself when reading code
            '  IE.document.getElementById("B91938241817236808").Click
                     'also works 

End Sub

标签: excelvbainternet-explorer

解决方案


它在输入而不是标签元素中,因此收集标签不会捕获您想要的内容。正如评论中所建议的那样,使用 id 是一种方法。如果找不到元素,请检查您的元素是否在需要协商的父框架或 iframe 中。一般语法是

ie.document.getElementsByTagName("frame")(appropriateIndexHere).contentDocument.getElementById("B91938241817236808")
ie.document.getElementsByTagName("iframe")(appropriateIndexHere).contentDocument.getElementById("B91938241817236808")

如果 id 是动态的,您可以使用该value属性

ie.document.querySelector("[value='Créer']")
ie.document.getElementsByTagName("frame")(appropriateIndexHere).contentDocument.querySelector("[value='Créer']") 'etc

由于有一个事件,您可能需要触发它。

ie.document.querySelector("[value='Créer']").FireEvent "onclick"
ie.document.getElementsByTagName("frame")(appropriateIndexHere).contentDocument.querySelector("[value='Créer']").FireEvent "onclick"  'etc

并使用适当的页面加载等待。所以这,

Do While IE.Busy                                                     'loading page
    Application.Wait DateAdd("s", 1, Now)
Loop

应该

While ie.Busy Or ie.ReadyState <> 4: DoEvents:Wend

推荐阅读