【问题标题】:Excel VBA - XMLHTTP: How to refer to button with no ID, only nameExcel VBA - XMLHTTP:如何引用没有 ID 的按钮,只有名称
【发布时间】:2017-11-26 18:22:06
【问题描述】:

我一直在尝试使用 Excel VBA 登录网站

用户名和密码字段工作正常

这是给我问题的点击按钮

按钮没有“ID”而是名称

查看图片:

查看突出显示的行

Dim siteDoc As Object
Set siteDoc = appIE.document

   siteDoc.getElementById("xName").Value = "user"
   siteDoc.getElementById("xPassword").Value = "password"
   siteDoc.getElementById("loginbutton").Click

我在 siteDoc.getElementById("loginbutton").Click 收到运行时 424 Object Required 错误

编辑: 在检查元素“控制台”中,它返回 node list

编辑:

什么有效:

Set buttonObject = siteDoc.getElementsByName("loginbutton")
For Each btnObj In buttonObject
    btnObj.Click
Next btnObj

【问题讨论】:

标签: html vba excel xmlhttprequest getelementbyid


【解决方案1】:

你可以找到按钮的集合(类名“button”)并循环,直到你得到名称属性“loginbutton”

  1. 分配 IHTMLElementCollection (Set Buttons= doc.getElementsByClassName("button"))

  2. 遍历Buttons 对象以找到登录按钮 - 您的整个代码将如下所示:

    子示例()

    设置按钮 = doc.getElementsByClassName("button")

    对于按钮中的每个 oElement If oElement.getAttribute("name") = "loginbutton" Then ' 这个 oElement 是你的按钮,所以你的动作在下面 '........ 万一 下一个

    结束子

oElementButtons 声明为对象

【讨论】:

  • 抱歉,我似乎无法更改格式以将我的代码显示为代码!
猜你喜欢
  • 2020-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 2016-04-26
  • 1970-01-01
  • 2015-11-02
  • 1970-01-01
相关资源
最近更新 更多