按AA Route Planner VBA中的Get Routebutton

我正在尝试在Internet Explorer中自动执行Route Plannersearch。 我有下面的代码是接近我所需要的,而不是按“获取路线”button,而是拿起“find一辆车”button。 一旦我得到这个sorting,我希望能够得到英里的距离的结果,并input回Excel。 谢谢。

我想我已经设法find我需要select的元素的代码,但不知道我如何去任何build议/帮助?

<a href="#" class="getRouteBtn" onclick="onGetRouteClicked(false);return false;" style="background-position: 0px -32px;"><span style="background-position: 100% -32px;">Get route</span></a>

 Public Sub route() Dim startpc As Object Dim endpc As Object Dim objie As SHDocVw.InternetExplorer 'microsoft internet controls (shdocvw.dll) Dim htmlDoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library Dim htmlInput As MSHTML.HTMLInputElement Dim htmlColl As MSHTML.IHTMLElementCollection Set objie = New SHDocVw.InternetExplorer With objie .navigate "http://www.theaa.com/route-planner/index.jsp" ' Main page .Visible = 1 Do While .readyState <> 4: DoEvents: Loop Application.Wait (Now + TimeValue("0:00:02")) Set startpc = objie.document.getElementById("routeFrom") Set endpc = objie.document.getElementById("routeTo") startpc.Value = ActiveCell.Value endpc.Value = ActiveCell.Offset(0, 1).Value 'click Button Set htmlDoc = .document Set htmlColl = htmlDoc.getElementsByTagName("input") Do While htmlDoc.readyState <> "complete": DoEvents: Loop For Each htmlInput In htmlColl If Trim(htmlInput.Type) = "submit" Then htmlInput.Click Exit For End If Next htmlInput End With End Sub 

PateBin链接

在这个循环中,你要求程序用一个“input”标签find第一个"submit"元素,然后点击它。

 For Each htmlInput In htmlColl If Trim(htmlInput.Type) = "submit" Then htmlInput.Click Exit For End If Next 

这是不正确的,因为“获取路线”button没有"input"标签,在Chrome浏览器开发工具中查看这个元素,它没有一个可靠的id属性,但你可以使用GetElementsByClassName("getRouteBtn")或者你可以使用它有一个IDdiv元素,如:

 '## Here, the .Children(0) will handle the actual button, which is the ' Div element's first child element htmlDoc.GetElementByID("getRouteWrapper").Children(0) 

在这里输入图像说明

单击该元素后,等待页面加载,并且生成的距离元素具有一个id属性,因此您可以使用GetElementByID("routeDistanceValue")

在这里输入图像说明

这似乎是工作,虽然请注意,我使用objie而不是ShDocVw.InternetExplorer InternetExplorer.Application类。

 Public Sub route() Dim startpc As Object Dim endpc As Object Dim objie As Object Dim htmlDoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library Dim htmlInput As MSHTML.HTMLInputElement Dim htmlColl As MSHTML.IHTMLElementCollection Set objie = CreateObject("InternetExplorer.Application") With objie .navigate "http://www.theaa.com/route-planner/index.jsp" ' Main page .Visible = True Call ReadyLoop(objie) Application.Wait (Now + TimeValue("0:00:02")) Set startpc = objie.document.GetElementByID("routeFrom") Set endpc = objie.document.GetElementByID("routeTo") startpc.Value = ActiveCell.Value endpc.Value = ActiveCell.Offset(0, 1).Value 'click Button Set htmlDoc = .document 'Find the "getRouteBtn" Dim btn As Object Set btn = htmlDoc.GetElementByID("getRouteWrapper").Children(0) If Not btn Is Nothing Then btn.Click Call ReadyLoop(htmlDoc) 'Get the distance item: Dim htmlDist As Object Set htmlDist = htmlDoc.GetElementByID("routeDistanceValue") If Not htmlDist Is Nothing Then ActiveCell.Offset(0, 2).Value = htmlDist.InnerText Else MsgBox "Element 'routDistanceValue' not found!", vbCritical GoTo EarlyExit End If End With Exit Sub EarlyExit: End Sub Sub ReadyLoop(obj As Object) Do While Not obj.ReadyState = 4 And Not obj.ReadyState = "complete" DoEvents Loop End Sub