IE click on a button that has no link associated t

2019-08-25 08:08发布

I want to click on a "button" on a web page, but my problem is that this button doesn't seem to have link attach to it. By the way I'm phrasing this you can see I'm not familiar at all with the language of web browser. Anyway I most use internet explorer and here's the code I have so far

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 

For sake of clarity here's the code of the web page around the "button" I'm trying to interact with. javascript code

I've made many research but I'm in a dead end right now. Any help will be greatly appreciate.
Tx in advance.

SOLVED FINAL CODE: With help from DOMENIC and QHARR and 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条回答
不美不萌又怎样
2楼-- · 2019-08-25 08:41

getElementsByClassName returns an array-like object of all child elements which have all of the given class names, so I think it should be IE.document.getElementsByClassName("rc-content-buttons")(0).Click if it is the first element with the classname.

You could also try: IE.document.querySelector("div.rc-content-buttons input").Click.

And I think IE.document.getElementById("B91938241817236808").Click is also right.

查看更多
相关推荐>>
3楼-- · 2019-08-25 08:50

It is within an input not a tag element so gathering a tags will not capture what you want. Using the id, as suggested in comments is one way to go. If you get element not found then check whether your element is within a parent frame or iframe which needs to be negotiated. General syntax for that is

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

If the id is dynamic you can use the value attribute

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

As there is an event, you may need to fire it.

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

And use a proper page load wait. So this,

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

Should be

While ie.Busy Or ie.ReadyState <> 4: DoEvents:Wend
查看更多
登录 后发表回答