VBA在IE中单击“登录”按钮

时间:2018-07-26 15:11:52

标签: html vba excel-vba internet-explorer

我对从IE中获取元素不太熟悉,但是当它简单明了时,我已经取得了成功。我正在使用的网站使我感到困惑。

我正在启动一个网站并输入用户名和密码,然后我想单击“登录”按钮,但由于找不到ID,因此找不到正确的定位方式并单击该按钮。

我在该网站上看到了各种帖子,都提出了类似的问题,并且我尝试按照这些帖子中的建议进行操作,但是仍然无法正常工作。

我将在站点上共享我的代码和元素信息。

Dim IEapp As InternetExplorerMedium
Dim divClassLogin As Object
Set IEapp = New InternetExplorerMedium

    With IEapp
        .Navigate "http://ctdayppv02/PVE.aspx"
        .Visible = True
        .AddressBar = 0
        .StatusBar = 0
        .Toolbar = 0
        .MenuBar = 0
        .Height = 700
        .Width = 950
    End With

    Do While IEapp.Busy: DoEvents: Loop
    Do Until IEapp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

    IEapp.Document.all.Item("txtUserName").Value = "Name"
    IEapp.Document.all.Item("txtPassword").Value = "Pass"
    IEapp.Document.all.Item("btnLogin").Click

这是IE Inspect Elements html。

IE HTML

这是Chrome Inspect Elements html。

Chrome HTML

我在本网站上引用的帖子(以及其他):

Click button on login page

VBA to click on a button in IE with no ID, Name, ClassName

Use VBA to click on a button in IE

有人可以帮助我确定单击登录时应关注的元素吗?我习惯于找到ID或显示“提交”的内容,但在此站点上看不到它。

谢谢。

1 个答案:

答案 0 :(得分:1)

因为我无法访问URL。我只是拿出了我通常会做的代码(未经测试),尽管对我来说很长,但对我来说却是最灵活的方式。请尝试:

Dim ee as Variant, htmldoc
Set htmldoc= IEapp.document
For each ee in htmldoc.getElementsByTagName("span")
   If ee.className = "dijitReset dijitInline dijitButtonNode" And InStr(ee.innerHTML, "btnLogin") > 0 Then 'not sure whether innerhtml or outerhtml you need tp check in your watches
      ee.Click: DoEvents: Sleep 1000
      ''or u can try this
      ee.FireEvent ("onclick"): DoEvents: Sleep 1000
      Exit For
    End If
 Next ee