exc*_*i11 3 html excel internet-explorer vba web-scraping
我正在尝试使用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
Run Code Online (Sandbox Code Playgroud)
因此,以下是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
Run Code Online (Sandbox Code Playgroud)
Tx Tim提供答案。现在,我可以选择第一个子窗体按钮将其打开。
links(j).click 'j = 0
Run Code Online (Sandbox Code Playgroud)
然后,将其保存,关闭,然后返回主窗体。但是当我尝试去做
links(j).click 'j = 2 this time
Run Code Online (Sandbox Code Playgroud)
第二次我收到运行时错误70:权限被拒绝。再次请您提供帮助。问候
在蒂姆·威廉姆斯(Tim Williams)的友好帮助下,我终于找到了遗漏的最后细节。这是下面的最终代码。
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
79519 次 |
| 最近记录: |