VBA 中的 GetElementById
GetElementById in VBA
我在使用我在网上找到并根据需要自定义的代码将数据从 Excel 输入 Web 浏览器时遇到问题。
他向我报告错误号 424,我不知道如何解决。
有没有可能是这个网站在自动输入数据的过程中被阻塞了?
这是来自网站的代码(检查):
输入 id="input_form1:j_idt26" name="input_form1:j_idt26" type="text" class="表单控制搜索框"
Sub SearchBot()
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim y As Integer 'integer variable we'll use as a counter
Dim result As String 'string variable that will hold our result link
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://nbs.rs/sr_RS/drugi-nivo-navigacije/servisi/registar-menica/"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'in the search box put cell "A2" value, the word "in" and cell "C1" value
objIE.document.getElementById("input_form1:j_idt26").Value = _
Sheets("Sheet1").Range("A2").Value
objIE.document.getElementById("input_form1:j_idt21").Value = _
Sheets("Sheet1").Range("A3").Value
您要查找的元素隐藏在 iframe 中,因此有点棘手,但这应该可行:
Sub SearchBot()
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim y As Integer 'integer variable we'll use as a counter
Dim result As String 'string variable that will hold our result link
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://nbs.rs/sr_RS/drugi-nivo-navigacije/servisi/registar-menica"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Dim ieDoc As MSHTML.HTMLDocument
Set ieDoc = objIE.Document
Dim iframeDoc As MSHTML.HTMLDocument
Set iframeDoc = ieDoc.frames(0).Document
'in the search box put cell "A2" value, the word "in" and cell "C1" value
iframeDoc.getElementById("input_form1:j_idt26").Value = _
Sheets("Sheet1").Range("A2").Value
iframeDoc.getElementById("input_form1:j_idt21").Value = _
Sheets("Sheet1").Range("A3").Value
End Sub
我在使用我在网上找到并根据需要自定义的代码将数据从 Excel 输入 Web 浏览器时遇到问题。 他向我报告错误号 424,我不知道如何解决。 有没有可能是这个网站在自动输入数据的过程中被阻塞了? 这是来自网站的代码(检查): 输入 id="input_form1:j_idt26" name="input_form1:j_idt26" type="text" class="表单控制搜索框"
Sub SearchBot()
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim y As Integer 'integer variable we'll use as a counter
Dim result As String 'string variable that will hold our result link
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://nbs.rs/sr_RS/drugi-nivo-navigacije/servisi/registar-menica/"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'in the search box put cell "A2" value, the word "in" and cell "C1" value
objIE.document.getElementById("input_form1:j_idt26").Value = _
Sheets("Sheet1").Range("A2").Value
objIE.document.getElementById("input_form1:j_idt21").Value = _
Sheets("Sheet1").Range("A3").Value
您要查找的元素隐藏在 iframe 中,因此有点棘手,但这应该可行:
Sub SearchBot()
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim y As Integer 'integer variable we'll use as a counter
Dim result As String 'string variable that will hold our result link
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://nbs.rs/sr_RS/drugi-nivo-navigacije/servisi/registar-menica"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Dim ieDoc As MSHTML.HTMLDocument
Set ieDoc = objIE.Document
Dim iframeDoc As MSHTML.HTMLDocument
Set iframeDoc = ieDoc.frames(0).Document
'in the search box put cell "A2" value, the word "in" and cell "C1" value
iframeDoc.getElementById("input_form1:j_idt26").Value = _
Sheets("Sheet1").Range("A2").Value
iframeDoc.getElementById("input_form1:j_idt21").Value = _
Sheets("Sheet1").Range("A3").Value
End Sub