簡體   English   中英

當按鈕沒有關聯的“名稱”時,使用Excel VBA單擊Internet Explorer中的按鈕

[英]Use Excel VBA to click on a button in Internet Explorer, when the button has no “name” associated

我正在嘗試使用excel自動化在時間表中輸入的值。 時間表在網頁上。 現在我可以加載頁面,輸入我的用戶名和密碼,然后單獨輸入時間表。 見下面的代碼。

此時我需要單擊一個按鈕來打開子表單。 我事先無法知道有多少子表格會打開。 我知道當它有一個“名字”時如何點擊一個按鈕。 但在這種情況下,沒有。 所以下面我更新的代碼使用循環打開每個其他子窗體。 它第一次工作,但是當我再次這樣做時

有人能指出我如何確定頁面中有多少按鈕以及如何點擊每個按鈕? 在我將代碼放到現在和將來之后,我需要與之交互的頁面的HTML代碼。

Private Sub time_sheet_filling()

    Dim I As Long
    Dim IE As Object
    Dim doc As Object
    Dim objElement As Object
    Dim objCollection As Object

    ' Create InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True

    ' Send the form data To URL As POST binary request
    IE.navigate "http://timesheet.cccc.ca/timesheet/"

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

        'Load the logon page
    Set objCollection = IE.Document.getElementsByTagName("input")
    I = 0
    While I < objCollection.Length
        If objCollection(I).Name = "txtUserName" Then
            ' Set text to enter
            objCollection(I).Value = "6666"
        End If
        If objCollection(I).Name = "txtPwd" Then
            ' Set text for password
            objCollection(I).Value = "password"
        End If
        If objCollection(I).Type = "submit" And objCollection(I).Name = "btnSubmit" Then ' submit button clicking
            Set objElement = objCollection(I)
        End If
        I = I + 1
    Wend

    objElement.Click    ' click button to load the form

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

    ' Show IE
    IE.Visible = True  
Dim links, link
Dim n, j
Set links = IE.Document.getElementById("dgTime").getElementsByTagName("a")
n = links.Length  
For j = 0 To n - 1 Step 2
    links(j).Click
'I have some operations to be done will post another question for this
IE.Document.getElementById"DetailToolbar1_lnkBtnSave").Click              'save
IE.Document.getElementById"DetailToolbar1_lnkBtnCancel").Click            'close
Next



End Sub  

所以html代碼的摘錄如下。 我正在嘗試單擊下面的html代碼的最后一行中編碼的按鈕

<table width="984" class="Grid" id="dgTime" border="1" rules="all" cellspacing="0">
  <tbody>
    <tr class="GridHeader">
    </tr>
    <tr class="GridItem">
    </tr>
    <tr class="GridItem">
      <td class="GridButtonColumn">
        <a href="javascript:__doPostBack('dgTime$_ctl2$_ctl0','')">
          <img src="images/toolbar/b_edit.gif">
        </a>
      </td  

Tx蒂姆的答案。 現在我可以選擇第一個子窗體按鈕來打開它。

links(j).click   'j = 0 

然后我保存,關閉,然后回到主窗體。 但是當我嘗試做的時候

links(j).click   'j = 2 this time

第二次我得到運行時錯誤70:權限被拒絕。 任何善意的幫助將非常感激。 問候

在蒂姆·威廉姆斯的親切幫助下,我終於找到了最后遺漏的東西。 這是下面的最終代碼。

Private Sub Open_multiple_sub_pages_from_main_page()


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


' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
' You can uncoment Next line To see form results
IE.Visible = True

' Send the form data To URL As POST binary request
IE.navigate "http://webpage.com/"

' Wait while IE loading...
While IE.Busy
        DoEvents
Wend


Set objCollection = IE.Document.getElementsByTagName("input")

i = 0
While i < objCollection.Length
    If objCollection(i).Name = "txtUserName" Then
        ' Set text for search
        objCollection(i).Value = "1234"
    End If
    If objCollection(i).Name = "txtPwd" Then
        ' Set text for search
        objCollection(i).Value = "password"
    End If

    If objCollection(i).Type = "submit" And objCollection(i).Name = "btnSubmit" Then ' submit button if found and set
        Set objElement = objCollection(i)
    End If
    i = i + 1
Wend
objElement.Click    ' click button to load page

' Wait while IE re-loading...
While IE.Busy
        DoEvents
Wend

' Show IE
IE.Visible = True
Set Doc = IE.Document

Dim links, link

Dim j As Integer                                                                    'variable to count items
j = 0
Set links = IE.Document.getElementById("dgTime").getElementsByTagName("a")
n = links.Length
While j <= n                                    'loop to go thru all "a" item so it loads next page
    links(j).Click
    While IE.Busy
        DoEvents
    Wend
    '-------------Do stuff here:  copy field value and paste in excel sheet.  Will post another question for this------------------------
    IE.Document.getElementById("DetailToolbar1_lnkBtnSave").Click              'save
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)                                   'wait
    Loop
    IE.Document.getElementById("DetailToolbar1_lnkBtnCancel").Click            'close
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)                                   'wait
    Loop
    Set links = IE.Document.getElementById("dgTime").getElementsByTagName("a")
    j = j + 2
Wend    
End Sub
IE.Document.getElementById("dgTime").getElementsByTagName("a")(0).Click

編輯:循環收集(項目應按照與源文檔中相同的順序出現)

Dim links, link 

Set links = IE.Document.getElementById("dgTime").getElementsByTagName("a")

'For Each loop
For Each link in links
    link.Click
Next link

'For Next loop
Dim n, i
n = links.length
For i = 0 to n-1 Step 2
    links(i).click
Next I

CSS選擇器:

使用img[src='images/toolbar/b_edit.gif']的CSS選擇器img[src='images/toolbar/b_edit.gif']

這表示選擇帶有img標簽的元素,其屬性src值為'images/toolbar/b_edit.gif'


CSS查詢:

CSS查詢


VBA:

您可以使用document.querySelector方法應用選擇器。

IE.document.querySelector("img[src='images/toolbar/b_edit.gif']").Click

暫無
暫無

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

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