我正在尝试通过vba-excel为以下链接创建IEautomation。
网址:http://qpldocs.dla.mil/search/default.aspx
代码包括搜索字符串" QPL-631",然后单击相应的java脚本链接MIL-I-631D(6)。当我检查" MIL-I-631D (6)"链接,我发现以下href标签的源代码
<a href="javascript:__doPostBack('search_list$DG$ctl03$ctl00','')">MIL-I-631D(6)</a>
因此href
链接没有点击选项,手动点击href
链接的地址与href地址完全不同。所以我被困在这里。我想添加一个代码点击&#34; MIL-I-631D(6)&#34;并输出结果。
我已经尝试了以下代码,到目前为止无法继续进行。
Private Sub IE_Autiomation()
Dim i As Long
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim ae As HTMLLinkElement
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "http://qpldocs.dla.mil/search/default.aspx"
Application.StatusBar = "Loading. Please wait..."
Do While IE.Busy = True Or IE.readyState <> 4: DoEvents: Loop
Application.StatusBar = "Search form submission. Please wait..."
IE.document.getElementById("Search_panel1_tbox").Value = "QPL-631"
IE.document.getElementById("Search_panel1_btn").Click
Do While IE.Busy = True Or IE.readyState <> 4: DoEvents: Loop
答案 0 :(得分:1)
以下是写入工作表的临时解决方案,因为您已经对产品代码“QPL-631”进行了硬编码,您可以直接使用URL字符串中的内容来返回结果。
注意:我从该页面中提取了表格ID:
html.getElementById("Lu_gov_DG")
您可能想要探索这是否是跨产品的共同主题(我怀疑是)。会让生活变得更轻松。你甚至可以完全取消IE,并寻求更快的XHR解决方案。
Option Explicit
Private Sub IE_Automation()
'References Internet Controls and HTML Object library
Dim i As Long
Dim IE As Object
Dim html As HTMLDocument
Dim product As String
product = "QPL-631"
Dim url As String
url = "http://qpldocs.dla.mil/search/parts.aspx?qpl=1528¶m=" & product & "&type=256"
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate url '"http://qpldocs.dla.mil/search/default.aspx"
Application.StatusBar = "Loading. Please wait..."
Do While .Busy = True Or .readyState <> 4: DoEvents: Loop
Set html = .Document
Dim allRowOfData As Object
Set allRowOfData = html.getElementById("Lu_gov_DG")
Dim r As Long, c As Long
Dim curHTMLRow As Object
For r = 1 To allRowOfData.Rows.Length - 1
Set curHTMLRow = allRowOfData.Rows(r)
For c = 0 To curHTMLRow.Cells.Length - 1
Cells(r + 1, c + 1) = curHTMLRow.Cells(c).innerText
Next c
Next r
.Quit
End With
Application.StatusBar = False 'And tidy up our change to the status bar
End Sub
有回发here的例子,我将会看一下。
参考:
答案 1 :(得分:0)
您有几种选择。请参阅下面的相关综合清单。
尝试获取锚标记的集合,其中包含:
GetElementsByTagName("a")
然后,使用尽可能多的逻辑迭代该集合,以确保您单击右键。
对于ie.document.getElementsByTagName(“a”)中的每个l
If l.ClassName = "hqt_button" Then
l.Click
Exit For
Next
如果有多个具有相同类名的锚,您可以这样做:
If l.ClassName = "hqt_button" AND l.Href = ""javascript:void(0): onclick=HeaderBox.trySubmit()" Then
l.Click
Exit For
Next
可选地
如果您使用的是IE9 +,则可以使用GetElementsByClassName方法。
GetElementsByClassName("hqt_button")