VBA IE点击/显示没有id或标签的javascript而不是超链接

时间:2016-04-03 15:08:49

标签: javascript vba internet-explorer

如何使用VBA点击/显示网站上没有ID或标记的javascript?

我正在使用Excel 2010以及这些引用: Visual Basic for Applications

Microsoft Excel 14.0对象库

OLE自动化

Microsoft Office 14.0 Ojbect Library

Microsoft HTML对象库

Microsoft Internet Controls

表格中有“显示详细信息”文字:

<table class="table table-striped cols20 schedule-table" id="container-no-1-1">
<colgroup>...</colgroup>
<tbody>
    <tr class="container-row">
    <td>...</td>
    <td>...</td>
    <td>...</td>
    <td>...</td>
    <td>...</td>
    <td class="hidden-print">
        <a title="Details to be displayed after Gate in full move" data-url="/trackingapp/transportplan?equipmentNo=ABCD1234567&amp;shipmentId=60G0KZBCBKY5C" class="pull-right show-toggle with-icon" data-target="#more_info_1_1" href="#more_info_1_1" data-toggle-text="Hide details"><i class="icon-expand"></i>"Show details"
        </a>
    </td>
    </tr>
</tbody>
</table>

href.click什么都不做:

Dim ObjIE As Object
Set ObjIE = CreateObject("Internetexplorer.Application")
ObjIE.navigate "www.whatever.com"
ObjIE.Visible = True
Do While ObjIE.Busy Or ObjIE.readyState <> 4: DoEvents: Loop
For Each objLink In IE.Document.GetElementsByTagName("HREF")
 If objLink.href ="#more_info_1_1" Then
  objLink.Click
  Exit For
 End If
Next objLink

也没有将URL放在字符串中:

Dim myURl As string, myString As String
myString = "ABCD1234567"
myURL = "http://www.whatever.com/search?searchNumber=" & myString
IE.navigate myURL

但是,一旦用户交互点击“显示详细信息”,HTML就是:

<table class="table table-striped cols20 schedule-table" id="container-no-1-1">
 <colgroup>...</colgroup>
<tbody><tr class="container-row">
    <td>...</td>
    <td>...</td>
    <td>...</td>
    <td>...</td>
    <td>...</td>        
    <td class="hidden-print">
        <a title="Details to be displayed after Gate in full move" data-url="/trackingapp/transportplan?equipmentNo=ABCD1234567&amp;shipmentId=60G0KZBCBKY5C" class="pull-right show-toggle with-icon" data-target="#more_info_1_1" href="#more_info_1_1" data-toggle-text="Hide details"><i class="icon-collapse"></i>Hide details</a>
     </td>
         <td class="hidden-print">
            <a title="Details to be displayed after Gate in full move" data-url="/trackingapp/transportplan?equipmentNo=ABCD1234567&amp;shipmentId=60G0KZBCBKY5C" class="pull-right show-toggle with-icon" data-target="#more_info_1_1" href="#more_info_1_1" data-toggle-text="Hide details">
                <i class="icon-collapse"></i>
                    Hide details
            </a>
        </td>                                       
    </tr>
</tbody></table>
<div class="show loaded" id="more_info_1_1">
<div class="row transport-plan-details" style="margin-left: 0;">
    ::before
<div class="offset1 transport-plan location-detail well well-white margin20right">
    <h4 class="location">
        <span class=""></span>
        "Singapore"
    </h4>
    <table class="row container-move past transport-plan-tbl cols20">
        ::before
        <colgroup>....</colgroup>
        <tbody><tr>
            <td></td>
            <td>
                <i class="icon-GATE-OUT-EMPTY"> 
                    ::before
                </i>
            </td>
            <td>
                "15 Feb 2016"
                    <br>
                    16:47"
            </td>
            <td>
                <strong>Gate out, empty</strong>
            </td>
            <td></td>
        </tr>
    </tbody>
        ::after
    </table>

所以,我正在寻找一种方法来点击“显示详细信息”,然后在VBA中返回“新加坡”,“2016年2月15日”,“16:47”和“门出,空”。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我试着回答。因为源html文件只有一个元素。您可以使用document.getElementsByTagname(“a”)(0)来获取元素。 如下所示:

set myEle=document.getElementsByTagname("a")(0)
myEle.click
Do While ObjIE.Busy Or ObjIE.readyState <> 4: DoEvents: Loop

同样,您可以找到tdstrong的数量并获取 他们的innerText

或者您可以尝试从http请求获取数据以捕获数据。

    url = "http://www.whatever.com/trackingapp/transportplan?equipmentNo=ABCD1234567&amp;shipmentId=60G0KZBCBKY5C"

    Set XMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    XMLHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    XMLHTTP.setRequestHeader "referer", "http://www.whatever.com"
    XMLHTTP.Open "GET", url, False
    XMLHTTP.send

    result = XMLHTTP.responseText
    result = trim(result)
    Set XMLHTTP = Nothing

将html捕获到变量后,您可以使用instrmid来剪切变量。

如果还不行,您可以尝试使用webquery来捕获数据。