使用vba自动单击一个页面上的按钮,然后单击另一个页面

时间:2018-01-24 13:10:36

标签: vba excel-vba internet-explorer excel

我正在尝试点击页面上的按钮\行,然后在新页面加载时点击其他内容。我可以让我的vba单击第一个按钮/链接,但是当我尝试点击下一个时我收到错误

  

运行时错误91:对象变量或未设置块变量

现在我必须诚实,我所拥有的代码来自研究,我完全不了解它,我已经搜索了所有试图纠正第二次点击,但似乎没有任何工作。

我到目前为止的代码是

Public Sub SHAREREPORTING()

Dim IE As SHDocVw.InternetExplorer
Dim Button As Object
Dim Button2 As Object


Set IE = New InternetExplorerMedium
IE.Visible = True
IE.navigate "URL"
Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop

Set Button = IE.Document.getElementById("b1_pki")
Button.Click

Application.Wait (Now + TimeValue("0:00:05"))

SendKeys "mypassword" , True
SendKeys "{ENTER}", True

Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop

Set Button2 = IE.Document.getElementById("seite-itm-2")

Application.Wait (Now + TimeValue("0:00:05"))

Button2.Click

End Sub

2个按钮的代码(我称之为按钮/链接,因为我不太确定它们是什么,我不懂javascript)我想点击此刻是< / p>

Button1的/链接1

<A onclick=loginViaPKI(); onkeypress=loginViaPKI(); id=b1_pki title="Log on with PKI - Button - To activate, press spacebar." class=urBtnEmph hideFocus style="WHITE-SPACE: nowrap" href="javascript:void(0);" ct="Button">Log on with PKI</A>

将Button2 /链路2

<TD onclick="return htmlbSL(this,7,'seite:sel_tab_2','','3','sel_tab_2')" id=seite-itm-2 class=urTbsLabelOff onkeydown="if (sapUrMapi_checkKey(event,'keydown',new Array('32'))){return htmlbSL(this,7,'seite:sel_tab_2','','3','sel_tab_2')};sapUrMapi_TabStrip_keySelect('seite',2,4,event);" noWrap><SPAN id=seite-itm-2-txt title="Bookmarks     " class=urTbsTxtOff>Bookmarks </SPAN></TD>

1 个答案:

答案 0 :(得分:0)

该计划需要参考以下内容:

1 Microsoft Internet Controls
2. Microsoft HTML Object Library

Internet控件用于浏览网页,HTML对象用于标识用户名和密码文本框,并使用控制按钮提交文本。

Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()

Dim oHTML_Element As IHTMLElement
Dim sURL As String

On Error GoTo Err_Clear
sURL = "https://www.google.com/accounts/Login"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True

Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE

Set HTMLDoc = oBrowser.Document

HTMLDoc.all.Email.Value = "sample@vbadud.com"
HTMLDoc.all.passwd.Value = "*****"

For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next

' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Debug.Assert Err = 0
Err.Clear
Resume Next
End If
End Sub