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

我试图用Excel来自动化时间表中input的值。 时间表在网页上。 现在我可以加载页面,input我的用户名和密码,然后input时间表本身。 看下面的代码。

在这一点上,我需要点击一个button来打开子表单。 我不能预先知道有多less个子表格可以打开。 我知道如何点击一个button,当它有一个“名称”。 但在这种情况下,没有。 所以我下面更新的代码使用循环来打开其他每个子窗体。 这是第一次,但是当我再做一次

有人能告诉我如何确定页面中有多less个button,以及如何点击每个button? 在我将代码放置到现在和之后,我需要与之交互的页面的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代码的最后一行编码的button

 <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 

发送蒂姆的答案。 现在我可以select第一个子窗体button来打开它。

 links(j).click 'j = 0 

然后我保存它,closures,然后回到主窗体。 但是当我尝试去做的时候

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

第二次我得到一个运行时错误70:权限被拒绝。 再次善良的帮助将如此赞赏。 问候

 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 

在蒂姆·威廉姆斯的亲切帮助下,我终于find了失踪的最后一个部分。 以下是最终的代码。

 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