简体   繁体   English

VBA对象必需错误尝试获取InnerText

[英]VBA Object Required Error Trying To Get InnerText

I am trying to create a code that will go to a website, put in data, submit it, and then return the answer to a cell in excel. 我正在尝试创建一个代码,该代码将转到网站,放入数据,提交,然后将答案返回到excel中的单元格。 When I step through it, it works fine, but when I just try to run it, I get run-time error 424; 当我逐步执行它时,它工作正常,但是当我尝试运行它时,出现运行时错误424。 Object Required. 所需对象。

I tried looking for a good answer, but I am just not grasping on how to quite fix this. 我试图寻找一个好的答案,但我只是不了解如何解决这个问题。 Where is my issue? 我的问题在哪里? How do I correct it? 我该如何纠正?

Sub Distance()


Dim IE As Object

' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")

' Make visible
IE.Visible = True

' Go to site
IE.Navigate "http://www.distance-cities.com/"

' Wait while IE loading...
Do Until IE.READYSTATE = 4
    DoEvents
Loop

IE.Document.getelementbyId("from").Value = "Stillwater, OK"
IE.Document.getelementbyId("to").Value = "Hollis, OK"
IE.Document.forms(0).submit

Do Until IE.READYSTATE = 4
    DoEvents
Loop

'*Below is where I get my error
Sheet1.Range("E5").Value = IE.Document.getelementbyId("routemi").InnerText
IE.Quit

End Sub

I apologize if this is a bit messy. 如果这有点混乱,我深表歉意。

Thanks for you help! 感谢您的帮助!

Something like this (untested): 像这样(未经测试):

Dim el as object
'...

Set el = WaitForElement(IE.Document, "routemi", 1)
If Not el is Nothing Then
    Sheet1.Range("E5").Value = el.innerText
Else
    Msgbox "Element 'routemi' not found!"
    Exit Sub
End if

Utility function to get an element by id, waiting until it appears: 实用程序函数,通过id获取元素,等待其出现:

Function WaitForElement(doc As Object, id As String, maxWaitSec As Double) As Object
    Dim rv As Object, t

    t = Timer
    Do
        Set rv = doc.getElementById(id)
        DoEvents
    Loop While rv Is Nothing And (Timer - t) < maxWaitSec

    Set WaitForElement = rv
End Function

You need to leave proper waits at each point (after .Navigate2 and .Click ) to allow for page loading. 你需要离开在每个点上适当的等待(后.Navigate2.Click )允许页面加载。 I use CSS selectors to target ids as modern browsers are optimized for CSS. 由于现代浏览器针对CSS进行了优化,因此我使用CSS选择器定位ID。 Also, I updated method for page navigation to .Navigate2 . 另外,我将页面导航的方法更新为.Navigate2

Option Explicit
Public Sub Distance()
    Dim ie As New InternetExplorer
    With ie
        .Visible = True

            .Navigate2 "http://www.distance-cities.com/"

            While .Busy Or .readyState < 4: DoEvents: Wend

            With .document
                .querySelector("#from").Value = "Stillwater, OK"
                .querySelector("#to").Value = "Hollis, OK"
                .querySelector("[type=submit]").Click
            End With

           While .Busy Or .readyState < 4: DoEvents: Wend
           Debug.Print .document.querySelector("#routemi").innerText
        .Quit
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM