网页上的VBA选择选项卡,它引用Java脚本

时间:2018-07-31 12:28:04

标签: javascript html vba web-scraping

我有以下HTML代码(请参见下文),我想单击此网页上名为NBT订单收据的按钮。除了单击此选项卡标题(选项卡后面没有URL,因此无法以这种方式导航)之外,我的代码适用于所有内容。我相信有用的代码在图像的两个突出显示的部分之间。

我尝试了查询选择器。单击带有名称和ID以及获取元素功能的按钮。

任何帮助将不胜感激(只需将1行代码添加到我的字符串中即可)

nb。网站是安全的,并且已登录,因此无法共享URL,因此无法共享代码图像。


HTML:

name="home_tab_name_97" value="KPI - Best Card Jobs"/></div>
<div id="home_tab_98" class="home_tab" title="" >
   <div class="home_tab_inner"><span id="home_tab_label_98" onclick="SelectHomeTab(98)">NBT Order Intake</span>&nbsp;<img src="Includes.aspx?type=image&amp;name=Images/lsd_down.gif" border="0" class="home_tab_setting" onclick="HomeTabSettings(98)" /></div>
   <div class="el_hidden" id="home_tab_settings_98">
      <div><a class="mnu_item" href="javascript: RenameHomeTab(98)">Rename</a></div>
      <div><a class="mnu_item" href="javascript: DeleteHomeTab(98)">Delete</a></div>
      <div><a class="mnu_item" href="javascript: ShareHomeTab(98)">Make Public</a></div>
      <div><a class="mnu_item" href="javascript: ShowAddDashCustomPageOptions(98)">Add Custom Item</a></div>
      <div class="mnu_item_divider"></div>
      <div><a class="mnu_item" onclick="HideHomeSettings(98)">Cancel</a></div>
   </div>
   <input type="hidden" id="home_tab_name_98" name="home_tab_name_98" value="NBT Order Intake"/>
</div>
<div id="home_tab_99" class="home_tab" title="" >
<div class="home_tab_inner">
<span id="home_tab_label_99"


HTML图片:

enter image description here


VBA:

Sub test()
    Dim IE As Object
    Dim objElement As Object
    Dim objCollection As Object

    'add worksheet
    Sheets.Add after:=ActiveSheet

    'destination
    Set destsheet = ActiveSheet

    'use internet explorer
    Set IE = CreateObject("InternetExplorer.application")
    ' with internet open, make this visable and go to webpage x, enter username a
    nd passwork
    With IE
        .Visible = True
        .navigate ("URL")
        While .Busy Or .readyState <> 4: DoEvents: Wend
        'ie.Document.getElementsbyname("User name").Focus
        IE.document.getElementsByName("username")(0).Value = "Username"
        IE.document.getElementsByName("password")(0).Value = "Pword"
        While .Busy Or .readyState <> 4: DoEvents: Wend
        'look for the input field (HTML CODE)
        Set objCollection = IE.document.getElementsByTagName("input")

        i = 0
        While i < objCollection.Length
            If objCollection(i).Type = "submit" And _
               objCollection(i).Name = "" Then
                ' "Search" button is found
                Set objElement = objCollection(i)

            End If
            i = i + 1
        Wend
        '(click the log on)
        objElement.Click
        While .Busy Or .readyState <> 4: DoEvents: Wend
    End With
    With IE
        IE.document.querySelector("input[id=home_tab_name_98]").Click
    End With
    'close the internet page
End Sub

1 个答案:

答案 0 :(得分:0)

您可以尝试:

ie.document.querySelector("#home_tab_label_98").Click
ie.document.querySelector("#home_tab_label_98").FireEvent "onclick"

ie.document.parentwindow.execscript "SelectHomeTab(98)"

元素的实际选择是通过CSS选择器进行的,其中#home_tab_label_98定位ID为home_tab_label_98的元素。 "#"用于ID。

CSS selector


如果找不到对象,则:

  1. 在尝试选择或尝试循环选择之前,请确保有一个暂停;
  2. 检查是否嵌套了任何先前的form / frame / iframe标签。需要导航这些标签才能访问元素;
  3. 记住任何点击事件或表单提交后,您需要While .Busy Or .readyState <> 4: DoEvents: Wend

仍然无法正常工作:

请显示更多html。