【问题标题】:Excel VBA: cannot Get inner text of HTML idExcel VBA:无法获取 HTML id 的内部文本
【发布时间】:2017-08-20 17:34:10
【问题描述】:

您好:我正在使用 excel 从该网页获取值:https://www2.agenciatributaria.gob.es/es13/h/iexmmmfi.html

如何获取 excel VBA 的代码以获取 NIF、EJF、MOD 和 CEL 的字段?

我尝试使用 getelementbyid("NIF") 和 "name" 但没有结果

谢谢!

上一个帖子:Excel VBA: Get inner text of HTML table td

这是我使用的代码:

Sub AEAT()
  Dim IE As Object
  Application.ScreenUpdating = False
  Set IE = CreateObject("InternetExplorer.Application")

  IE.Navigate "https://www2.agenciatributaria.gob.es/es13/h/iexmmmfi.html"

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


  IE.Document.getElementById("NIF").Value = Range("A1").Value
  Application.Wait (Now + TimeValue("0:00:01"))

  IE.Document.getElementById("EJF").Value = "2016"
  Application.Wait (Now + TimeValue("0:00:01"))

  IE.Document.getElementById("MOD").Value = "347"
  Application.Wait (Now + TimeValue("0:00:01"))

  IE.Document.getElementById("CEL").Value = Range("a4").Value
  Application.Wait (Now + TimeValue("0:00:01"))

  IE.Document.getElementById("env_button").Click

End Sub

【问题讨论】:

  • 贴出你用过的代码
  • 已添加代码。谢谢!
  • 您的问题可能更为根本。尝试使用此 URL 打开浏览器,打开控制台(通常按 F12)并输入document.getElementById('NIF')。你得到某种结果吗?一旦你可以在 VBA 之外成功使用 DOM,在 VBA 中转换代码就相对简单了。
  • 另外,我强烈建议您包含您尝试解析的 HTML。就我而言,我运行的是没有 Java 的浏览器,因此看不到您要解析的网页。
  • 好的,谢谢。我会做的

标签: html excel vba


【解决方案1】:

好的,所以我在 Windows VM 中没有 Excel,所以我使用 VBScript 测试了您的代码。所以解决方案应该有效。你有 3 个问题

  1. 导航不等待页面加载
  2. 您的站点安装了一个 ActiveX 控件,需要先加载该控件
  3. 即使您修复了 #1 和 #2,您也会收到异常“客户端已断开连接”并且该对象将不再可用

解决方案

在您的 IE 设置中将该站点添加到受信任的站点。

手动打开 IE 并安装 ActiveX 控件

Sub AEAT()
  Dim IE
  Set IE = CreateObject("InternetExplorer.Application")
  IE.visible = True
  hwnd = ie.hwnd
  IE.Navigate "https://www2.agenciatributaria.gob.es/es13/h/iexmmmfi.html"
  msgbox ("wait")

  Set oShell = CreateObject("Shell.Application")

  For Each Wnd In oShell.Windows
         If hwnd = Wnd.hwnd Then Set ie = Wnd
  Next 
  IE.Document.getElementById("NIF").Value = "123"
End Sub

Call AEAT

我已将 msgbox 插入等待,但您可以通过在 excel 中使用下面的方法自行修复此问题

  Do
    DoEvents
  Loop Until ie.ReadyState = READYSTATE_COMPLETE

PS:参考以下网址

Internet Explorer VBA Automation Error: The object Invoked has disconnected from its clients

Error "The object invoked has disconnected from its clients" - automate IE 8 with python and win32com

【讨论】:

  • 太棒了!非常感谢!但是我仍然对“MOD”或“ID_MOD”字段有问题,使用get elementbyID或byname都不起作用。
  • MOD 只有名字所以你需要使用GetElementsByName,它返回一个数组而不是一个对象。所以你需要使用document.getElementsByName("MOD")[0].value = "038"
  • 对不起,它在 excel VBA 中返回“语法错误”。 getElementsByName("MOD")[0]
  • Excel VBA 代码:Sub AEATWEB() Dim IE Set IE = CreateObject("InternetExplorer.Application") IE.Visible = True Hwnd = IE.Hwnd IE.Navigate "www2.agenciatributaria.gob.es/es13/h/iexmmmfi.html" MsgBox ("等待") 为 oShell.Windows 中的每个 Wnd 设置 oShell = CreateObject("Shell.Application") If Hwnd = Wnd.Hwnd Then Set IE = Wnd Next IE.Document.getElementById("EJF").Value = Range("A2 ").Value IE.Document.getElementsByName("MOD")[0].value = "347" End Sub
  • 先将值放入另一个变量,然后对其使用索引
猜你喜欢
  • 2013-12-10
  • 2021-07-12
  • 2020-12-23
  • 2020-07-05
  • 1970-01-01
  • 2018-05-28
  • 2014-06-13
  • 2018-05-22
  • 1970-01-01
相关资源
最近更新 更多