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

编程入门 行业动态 更新时间:2024-10-26 13:20:44
本文介绍了当按钮没有“名称"时,使用 Excel VBA 单击 Internet Explorer 中的按钮.联系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 excel 自动输入时间表中的值.时间表在网页上.现在我可以加载页面,输入我的用户名和密码,然后自己输入时间表.请参阅下面的代码.

I'm trying to use excel to automate the value entering in a time sheet. 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

有人能告诉我如何确定页面中有多少个按钮以及如何点击每个按钮吗?下面我将我现在拥有的代码放在它下面,我需要与之交互的页面的 HTML 代码.

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.

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 "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 代码最后一行中编码的按钮

So extract of the html code is below. I'm trying to click the button that is coded in the last line of the html code below

<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 的答案.现在我可以选择第一个子表单按钮来打开它.

Tx Tim for the answers. 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

我第二次收到运行时错误 70:权限被拒绝.任何更多的帮助将不胜感激.问候

the second time I get a runtime error 70: permission denied. 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 "webpage/" ' 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

更多推荐

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

本文发布于:2023-06-09 04:10:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/594952.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   单击   名称   quot   Excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!