VBA:无法使用 getelementsbyTagname().Value 在 <input> 标签内提取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26832431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA: not able to pull value within <input> tag using getelementsbyTagname().Value
提问by pexpex223
This is my first post on stackflow :) I've been Googling VBA knowledge and writing some VBA for about a month.
这是我关于 stackflow 的第一篇文章 :) 我一直在谷歌搜索 VBA 知识并编写一些 VBA 大约一个月。
My computer info:
我的电脑信息:
1.window 8.1
1.窗口 8.1
2.excel 2013
2.excel 2013
3.ie 11
3.即11
My excel reference
我的excel参考
Microsoft Object Library: yes
Microsoft 对象库:是
Microsoft Internet Controls: yes
Microsoft Internet 控件:是
Microsoft Form 2.0 Object library: yes
Microsoft Form 2.0 对象库:是
Microsoft Script Control 1.0: yes
Microsoft Script Control 1.0:是
Issue:
问题:
I was trying to retrieve data from internet explorer automatically using VBA. I would like to retrieve the value within an input tag from a id called "u_0_1" which is under a id called "facebook". I am expecting to retrieve the value "AQFFmT0qn1TW" on cell c2. However, it got this msg popped up after I run the VBA "run-time error '91':object variable or with block variable not set. I have been trying this for a couple of weeks using different methods such as,
我试图使用 VBA 从 Internet Explorer 自动检索数据。我想从名为“u_0_1”的 ID 中检索输入标签中的值,该 ID 位于名为“facebook”的 ID 下。我期望在单元格 c2 上检索值“AQFFmT0qn1TW”。但是,在我运行 VBA“运行时错误'91':对象变量或未设置块变量后,它弹出了这个消息。我已经使用不同的方法尝试了几个星期,例如,
1.getelementsbyClassname
1.getelementsbyClassname
2.getelementbyid
2.getelementbyid
3.getelementsbyTagname
3.getelementsbyTagname
But it just doesn't work.
但这不起作用。
url:
网址:
http://coursesweb.net/javascript/getelementsbytagname
http://coursesweb.net/javascript/getelementsbytagname
Below is my VBA code. Could you guys help me out a little bit please?
下面是我的 VBA 代码。请你们帮我一下好吗?
Private Sub CommandButton1_Click()
Dim ie As Object
Dim Doc As HTMLDocument
Dim getThis As String
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = 0
ie.navigate "http://coursesweb.net/javascript/getelementsbytagname"
Do
DoEvents
Loop Until ie.readyState = 4
Set Doc = ie.document
getThis = Trim(Doc.getElementById("u_0_1")(0).getElementsByTagName("input")(0).Value)
Range("c2").Value = getThis
End Sub
回答by pexpex223
Thanks for your help. I have no idea that there is difference between JS and VBA in aspect of getelementsby () methods. And using the loop method to find the id which I find it very useful as well.
谢谢你的帮助。我不知道 JS 和 VBA 在 getelementsby() 方法方面存在差异。并使用循环方法查找 id,我发现它也非常有用。
I still have some issues to retrieve value from a form or input type. I hope that you could help me or give me some suggestions as well.
我仍然有一些问题要从表单或输入类型中检索值。我希望你能帮助我或给我一些建议。
Expected Result:
预期结果:
retrieve the value "AQFFmT0qn1TW" and copy it on Cell ("c2") automatically.
检索值“AQFFmT0qn1TW”并自动将其复制到单元格(“c2”)上。
Actual Result:
实际结果:
nothing return to Cell ("C2")
没有返回到单元格(“C2”)
Below is the HTML elements.
下面是 HTML 元素。
<form rel="async" ajaxify="/plugins/like/connect" method="post" action="/plugins/like/connect" onsubmit="return window.Event && Event.__inlineSubmit && Event.__inlineSubmit(this,event)" id="u_0_1">
<input type="hidden" name="fb_dtsg" value="AQFFmT0qn1TW" autocomplete="off">
Below is the VBA code based on your code.
以下是基于您的代码的 VBA 代码。
Private Sub CommandButton1_Click()
Dim ie As Object
Dim Doc As HTMLDocument
Dim Elements As IHTMLElementCollection
Dim Element As IHTMLElement
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = 0
ie.navigate "http://coursesweb.net/javascript/getelementsbytagname"
Do
DoEvents
Loop Until ie.readyState = 4
Set Doc = ie.document
Set Elements = Doc.getElementsByTagName("input")
For Each Element In Elements
If Element.name = "fb_dtsg" Then
Range("c2").Value = Element.innerText
End If
Next Element
Set Elements = Nothing
End Sub
Cheers.
干杯。
回答by Rafal Wegner
first of all, I can't find in source of website tags you were searching. Anyway, I think you can't chain getElementById.getElementsByTag as in JS. You have to loop through collection of document elements.
首先,我在您搜索的网站标签来源中找不到。无论如何,我认为你不能像在 JS 中那样链接 getElementById.getElementsByTag。您必须遍历文档元素的集合。
Private Sub CommandButton1_Click()
Dim ie As Object
Dim Doc As HTMLDocument
Dim Elements As IHTMLElementCollection
Dim Element As IHTMLElement
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = 0
ie.navigate "http://coursesweb.net/javascript/getelementsbytagname"
Do
DoEvents
Loop Until ie.readyState = 4
Set Doc = ie.document
Set Elements = Doc.getElementsByTagName("ul")
For Each Element In Elements
If Element.ID = "ex4" Then
Sheets(1).Cells(1, 1).Value = Element.innerText
End If
Next Element
Set Elements = Nothing
End Sub
First I'm getting collection of tags "ul", then looping through them for id "ex4". In your case you'd get collection of "input"s then loop for id you want. Finding id which is followed by different id shouldn't be hard, just some if...then
s.
首先我得到标签“ul”的集合,然后循环遍历它们以获取id“ex4”。在你的情况下,你会得到“输入”的集合,然后循环你想要的 id。找到后跟不同 id 的 id 应该不难,只是一些if...then
s。
If you need further assistant please respond with url in which I can find exactly what you're looking for.
如果您需要更多助手,请回复网址,我可以在其中准确找到您正在寻找的内容。
Cheers
干杯