簡體   English   中英

IE單擊沒有鏈接的按鈕(使用Excel VBA)

[英]IE click on a button that has no link associated to it (using Excel VBA)

我想單擊網頁上的“按鈕”,但是我的問題是此按鈕似乎沒有鏈接。 順便說一下,您可以看到我對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代碼

我進行了許多研究,但我現在處於死胡同。 任何幫助將不勝感激。
提前發送。

解決的最終代碼:在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

getElementsByClassName返回具有所有給定類名的所有子元素的類似數組的對象,因此我認為它應為IE.document.getElementsByClassName("rc-content-buttons")(0).Click是否為第一個具有類名的元素。

您也可以嘗試: IE.document.querySelector("div.rc-content-buttons input").Click

而且我認為IE.document.getElementById("B91938241817236808").Click也是正確的。

它在輸入中,而不是標簽元素,因此收集標簽不會捕獲您想要的內容。 按照注釋中的建議使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM