Javascript 如何使用VBA定位onmouseover元素?

Javascript 如何使用VBA定位onmouseover元素?,javascript,excel,vba,web-scraping,Javascript,Excel,Vba,Web Scraping,我正试图刮一个网站,有一个元素,如果你把鼠标移到它上面,它会在一个气泡中显示一些信息。我正在使用VBA来抓取页面,但我不知道如何定位特定的元素 查看页面的来源,我发现: 举个例子: Option Explicit Sub Test() Dim objIE, colTdNodes, i, objTdNode, objDivNode, strTooltipContent, objDispNode ' open the page Set objIE = CreateObj

我正试图刮一个网站,有一个元素,如果你把鼠标移到它上面,它会在一个气泡中显示一些信息。我正在使用VBA来抓取页面,但我不知道如何定位特定的元素

查看页面的来源,我发现:


举个例子:

Option Explicit

Sub Test()
    Dim objIE, colTdNodes, i, objTdNode, objDivNode, strTooltipContent, objDispNode

    ' open the page
    Set objIE = CreateObject("InternetExplorer.Application")
    With objIE
        .Visible = True
        .Navigate "http://www.oddsportal.com/baseball/usa/mlb-2014/arizona-diamondbacks-st-louis-cardinals-jeOoAP9r/"
        ' wait until IE and the page are ready
        Do While .Busy Or Not .readyState = 4: DoEvents: Loop
        ' wait until the DOM is ready
        Do Until .document.readyState = "complete": DoEvents: Loop
        ' wait until the table is ready
        Do While TypeName(.document.getElementById("odds-data-table")) = "Null": DoEvents: Loop
    End With

    ' below is an example how to retrieve tooltips content

    ' get table target cells nodes collection
    Set colTdNodes = objIE.document.getElementsByClassName("right odds")
    ' loop through each cell in collection
    For i = 0 To colTdNodes.Length - 1
        ' choose the cell from collection
        Set objTdNode = colTdNodes(i)
        ' get div node from the cell
        Set objDivNode = objTdNode.ChildNodes.Item(0)
        ' get the tooltip content
        strTooltipContent = GetTooltipContent(objDivNode)
        ' create new div node to display tooltip content
        Set objDispNode = objIE.document.createElement("div")
        ' add the created node into the cell
        objTdNode.appendChild objDispNode
        ' set id and style
        objDispNode.ID = "tooltip" & i
        objDispNode.Style.Background = "#ddd"
        objDispNode.Style.padding = "5px"
        objDispNode.Style.margin = "5px"
        ' display the tooltip content in the node
        objDispNode.innerHtml = strTooltipContent
    Next
    ' hide the last tooltip
    objIE.document.parentWindow.execScript "delayHideTip();", "javascript"
End Sub

Function GetTooltipContent(objNode)
    Dim objEventMouseOver, objTipNode, objDocument
    ' get document object
    Set objDocument = objNode.OwnerDocument
    ' create mouse event object
    Set objEventMouseOver = objDocument.createEvent("MouseEvents")
    ' setup mouseover event
    objEventMouseOver.initMouseEvent "mouseover", True, True, objDocument.parentWindow, 1, 12, 345, 7, 220, False, False, True, False, 0, ""
    ' send mouseover event to the div node
    ' support for dispatchEvent was added in IE9
    objNode.dispatchEvent objEventMouseOver
    ' retrieve appeared tooltip node
    Set objTipNode = objDocument.getElementById("tooltiptext")
    ' get tooltip html content
    GetTooltipContent = objTipNode.innerHtml
End Function

不幸的是,我无法访问当前网络上的网站,但我想知道工具提示信息是否存储在HTML的其他位置?我没有经常使用工具提示信息,但是一旦我这样做了,并且信息在页面下方的HTML中,它就使用CSS和JS来控制信息的显示。
Option Explicit

Sub Test()
    Dim objIE, colTdNodes, i, objTdNode, objDivNode, strTooltipContent, objDispNode

    ' open the page
    Set objIE = CreateObject("InternetExplorer.Application")
    With objIE
        .Visible = True
        .Navigate "http://www.oddsportal.com/baseball/usa/mlb-2014/arizona-diamondbacks-st-louis-cardinals-jeOoAP9r/"
        ' wait until IE and the page are ready
        Do While .Busy Or Not .readyState = 4: DoEvents: Loop
        ' wait until the DOM is ready
        Do Until .document.readyState = "complete": DoEvents: Loop
        ' wait until the table is ready
        Do While TypeName(.document.getElementById("odds-data-table")) = "Null": DoEvents: Loop
    End With

    ' below is an example how to retrieve tooltips content

    ' get table target cells nodes collection
    Set colTdNodes = objIE.document.getElementsByClassName("right odds")
    ' loop through each cell in collection
    For i = 0 To colTdNodes.Length - 1
        ' choose the cell from collection
        Set objTdNode = colTdNodes(i)
        ' get div node from the cell
        Set objDivNode = objTdNode.ChildNodes.Item(0)
        ' get the tooltip content
        strTooltipContent = GetTooltipContent(objDivNode)
        ' create new div node to display tooltip content
        Set objDispNode = objIE.document.createElement("div")
        ' add the created node into the cell
        objTdNode.appendChild objDispNode
        ' set id and style
        objDispNode.ID = "tooltip" & i
        objDispNode.Style.Background = "#ddd"
        objDispNode.Style.padding = "5px"
        objDispNode.Style.margin = "5px"
        ' display the tooltip content in the node
        objDispNode.innerHtml = strTooltipContent
    Next
    ' hide the last tooltip
    objIE.document.parentWindow.execScript "delayHideTip();", "javascript"
End Sub

Function GetTooltipContent(objNode)
    Dim objEventMouseOver, objTipNode, objDocument
    ' get document object
    Set objDocument = objNode.OwnerDocument
    ' create mouse event object
    Set objEventMouseOver = objDocument.createEvent("MouseEvents")
    ' setup mouseover event
    objEventMouseOver.initMouseEvent "mouseover", True, True, objDocument.parentWindow, 1, 12, 345, 7, 220, False, False, True, False, 0, ""
    ' send mouseover event to the div node
    ' support for dispatchEvent was added in IE9
    objNode.dispatchEvent objEventMouseOver
    ' retrieve appeared tooltip node
    Set objTipNode = objDocument.getElementById("tooltiptext")
    ' get tooltip html content
    GetTooltipContent = objTipNode.innerHtml
End Function