IE单击没有链接的按钮(使用Excel VBA)

时间:2019-08-06 20:23:55

标签: excel vba internet-explorer

我想单击网页上的“按钮”,但是我的问题是此按钮似乎没有链接。顺便说一下,您可以看到我对Web浏览器的语言完全不熟悉。 无论如何,我最经常使用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 code

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

解决的最终代码:在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

2 个答案:

答案 0 :(得分:1)

x 0.5 1.815 2.5 返回具有所有给定类名的所有子元素的类似数组的对象,因此,如果它是具有类名的第一个元素,则我认为它应该是getElementsByClassName。 >

您还可以尝试:IE.document.getElementsByClassName("rc-content-buttons")(0).Click

我认为IE.document.querySelector("div.rc-content-buttons input").Click也正确。

答案 1 :(得分:1)

它在输入中而不是标签元素,因此收集标签不会捕获您想要的内容。按照注释中的建议使用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