javascript - 如何使用 VBA 定位 onmouseover 元素?

标签 javascript excel vba web-scraping

我正在尝试抓取一个网站,并且有一个元素,如果您将鼠标移到它上面,它会在气泡中显示一些信息。我正在使用 VBA 抓取页面,但我不知道如何找到特定元素。

查看页面源代码,我得到以下信息:

<td class="right odds up">
<div onmouseout="delayHideTip()" onmouseover="page.hist(this,'P-0.00-00','1q3cfx2rrhkx0x3jtah',381,event,0,1)"

提供一些详细信息,以下是我要抓取的页面:match page.当您将鼠标移到箭头和数字上时,会出现一个包含一些内容的矩形。这就是我想要得到的。

我的代码:

Private Sub CommandButton6_Click()

the_start:
Dim objIE As Object

Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True

On Error Resume Next

objIE.Navigate ("http://www.oddsportal.com/baseball/usa/mlb-2014/arizona-diamondbacks-st-louis-cardinals-jeOoAP9r/")

Do
DoEvents
    If Err.Number <> 0 Then
        objIE.Quit
        Set objIE = Nothing
        GoTo the_start:
    End If
Loop Until objIE.ReadyState = 4



objIE.Document.all.Item
Dim js As Variant
js = page.hist(this,P-0.00-0-0,1q3cfx2rrhkx0x3jtah,381,event,0,1) ???
Call objIE.Document.parentWindow.execScript(js, "JavaScript")  ???

MsgBox "objIE.js"

End Sub

最佳答案

考虑这个例子:

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

关于javascript - 如何使用 VBA 定位 onmouseover 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32567178/

相关文章:

excel - 地址上方首次使用的单元格

excel - 每次迭代增加范围

vba - 使用 Excel VBA 运行 CMD 命令

javascript - 如何修复 JSON.parse() 命令中不需要的异常

javascript - 为什么 `{ a: string }`不能流入 `{ a?: string }`

javascript - document.observe ("dom:loaded") 窗口处于非事件状态时不会触发

Java POI 提供的数据似乎位于 Office 2007+ XML 中

java - 当某些行已经存在时,Excel POI 意外丢失行

arrays - 在数组 VBA 中查找特定 Object.Name 字符串的快速方法

javascript - 在回发 Bootstrap 选项卡后使当前选项卡处于事件状态