简体   繁体   English

当按钮没有关联的“名称”时,使用Excel VBA单击Internet Explorer中的按钮

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

I'm trying to use excel to automate the value entering in a time sheet. 我正在尝试使用excel自动化在时间表中输入的值。 The time sheet is on a web page. 时间表在网页上。 Right now I'm able to load the page, enter my username and password and then entering the time sheet by itself. 现在我可以加载页面,输入我的用户名和密码,然后单独输入时间表。 See code below. 见下面的代码。

At this point I need to click on a button to open sub-forms. 此时我需要单击一个按钮来打开子表单。 I can't know in advance how many sub-forms there will be to open. 我事先无法知道有多少子表格会打开。 I know how to click on a button when it has a "name". 我知道当它有一个“名字”时如何点击一个按钮。 But in this case there's none. 但在这种情况下,没有。 So my updated code below use a loop to open every other subform. 所以下面我更新的代码使用循环打开每个其他子窗体。 It works the first time, but when I do it again 它第一次工作,但是当我再次这样做时

Could someone point me how to determine how many of those button there is in the page and how to click on each? 有人能指出我如何确定页面中有多少按钮以及如何点击每个按钮? Following I'm placing the code I have until now and below it, the HTML code of the page I need to interact with. 在我将代码放到现在和将来之后,我需要与之交互的页面的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  

So extract of the html code is below. 所以html代码的摘录如下。 I'm trying to click the button that is coded in the last line of the html code below 我正在尝试单击下面的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 Tim for the answers. Tx蒂姆的答案。 Now I'm able to select the first subform button to open it. 现在我可以选择第一个子窗体按钮来打开它。

links(j).click   'j = 0 

I then save it, close, and come back to the main form. 然后我保存,关闭,然后回到主窗体。 But then when I try to do 但是当我尝试做的时候

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

the second time I get a runtime error 70: permission denied. 第二次我得到运行时错误70:权限被拒绝。 Anymore kind help will be so appreciated. 任何善意的帮助将非常感激。 Regards 问候

With the kind help from Tim Williams, I finally figured out the last détails that were missing. 在蒂姆·威廉姆斯的亲切帮助下,我终于找到了最后遗漏的东西。 Here's the final code below. 这是下面的最终代码。

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

EDIT: to loop through the collection (items should appear in the same order as they are in the source document) 编辑:循环收集(项目应按照与源文档中相同的顺序出现)

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 selector: CSS选择器:

Use a CSS selector of img[src='images/toolbar/b_edit.gif'] 使用img[src='images/toolbar/b_edit.gif']的CSS选择器img[src='images/toolbar/b_edit.gif']

This says select element(s) with img tag with attribute src having value of 'images/toolbar/b_edit.gif' 这表示选择带有img标签的元素,其属性src值为'images/toolbar/b_edit.gif'


CSS query: CSS查询:

CSS查询


VBA: VBA:

You can apply the selector with the .querySelector method of document . 您可以使用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