【问题标题】:Automate clicking buttons on one page and then another page using vba使用 vba 自动单击一个页面上的按钮,然后单击另一个页面
【发布时间】:2018-01-24 13:10:35
【问题描述】:

我正在尝试处理单击页面上的按钮\行,然后在新页面加载时单击其他内容。我可以让我的 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)我现在尝试点击的是

按钮1/链接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>

按钮2/链接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>

【问题讨论】:

    标签: vba excel internet-explorer


    【解决方案1】:

    该程序需要参考以下内容:

    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
    

    【讨论】:

    • 谢谢 ryguy - 但我认为这只会登录到页面 - 我已经从中获取了一些指示来改进我的代码,但是我试图让我的代码点击页面上的内容在登录页面之后加载。我认为(不要引用我的话)问题出在我的代码中,IE 是一种新的 Internet Explorer 媒体,当第二页加载时我使用的是 IE.Document。
    猜你喜欢
    • 2011-12-15
    • 2015-11-24
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    相关资源
    最近更新 更多