【问题标题】:GetElementById in VBAVBA 中的 GetElementById
【发布时间】:2021-09-28 02:11:17
【问题描述】:

我在使用网上找到的代码将数据从 Excel 输入到 Web 浏览器时遇到问题,并根据我的需要对其进行了自定义。 他向我报告错误号 424,我不知道如何解决。 在自动数据输入过程中,该站点是否可能出现阻塞? 这是来自网站的代码(检查): 输入 id="input_form1:j_idt26" name="input_form1:j_idt26" type="text" class="form-control searchbox"

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

【问题讨论】:

  • 错误发生在哪里? objIE.document.getElementById("input_form1:j_idt26") 返回什么?
  • 错误出现在:objIE.document.getElementById("input_form1:j_idt26").Value = Sheets("Sheet1").Range("A2").Value 没有回来。跨度>

标签: vba getelementbyid


【解决方案1】:

您要查找的元素隐藏在 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

【讨论】:

  • 非常感谢,现在可以正常使用了。
  • @Stevinho 很高兴知道!如果此答案足以满足您的问题,请点击旁边的点击接受它。
猜你喜欢
  • 2019-10-07
  • 2016-07-04
  • 1970-01-01
  • 1970-01-01
  • 2015-09-20
  • 2019-10-28
  • 2020-05-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多