Excel IE 自动化识别 "css" 元素

Excel IE Automation identifying a "css" element

仍在抓取网站、从网页打印等方面进行掠夺,但 运行 陷入困境我希望有人能帮助我。下图显示了我想要激活的选项卡,我有代码可以让我在那里,但无法激活该选项卡。虽然最后一行代码可以在我的台式电脑上运行,但我无法让它在我的其他电脑上运行。我可能可以将 IE 并置在其他 PC 上以使其工作,但我知道我只是识别了错误的元素,如果我做对了它应该一直工作而不必弄乱 IE 设置。

带有“位置报告”选项卡的网站图片如下

在不同的浏览器上使用 Selenium 我能够确定该选项卡被识别如下::

css=#ext-gen42 .x-tab-strip-text
xpath=//a[@id='ext-gen42']/em/span/span
xpath=//div[3]/div/div/div/div/div/ul/li[2]/a[2]/em/span/span

我的代码在这里:

Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)
    Dim theEvent As Object
    htmlElementWithEvent.Focus
    Set theEvent = htmlDocument.createEvent("HTMLEvents")
    theEvent.initEvent eventType, True, False
    htmlElementWithEvent.dispatchEvent theEvent
End Sub
Sub Oasis_Test()
    LocnAddr = "1 park Ave"
    LocnBoro = "Manhattan"
    Dim IE As Object
    Dim htmlDoc As Object
    Dim Field1 As Object
    Dim Field2 As Object
    Dim Field3 As Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.Navigate "http://www.oasisnyc.net/map.aspx"
    Do Until IE.readyState = 4: DoEvents: Loop
    Application.Wait (Now + TimeSerial(0, 0, 3))
    'ShowWindow IE.hWnd, SW_SHOWMAXIMIZED   ' off for test purposes
    Set htmlDoc = IE.document
    On Error Resume Next
    Set Field1 = htmlDoc.getElementById("ext-comp-1076")
    On Error GoTo 0
    Call TriggerEvent(htmlDoc, Field1, "compositionstart")
    Field1.value = LocnAddr
    Call TriggerEvent(htmlDoc, Field1, "compositionend")
    Application.Wait (Now + TimeSerial(0, 0, 2))
    ' there's probably a better way to do this drop-down but this works
    Set Field2 = htmlDoc.getElementById("ext-comp-1078")
    Call TriggerEvent(htmlDoc, Field2, "compositionstart")
    Field2.value = LocnBoro
    Call TriggerEvent(htmlDoc, Field2, "compositionend")
    Application.SendKeys "{TAB}"
    Application.Wait (Now + TimeSerial(0, 0, 2))
    htmlDoc.getElementById("ext-gen118").Click
    Application.Wait (Now + TimeSerial(0, 0, 2))
    ' I don't get an error on the next line and it actually works on my desktop
    ' but can't get it to work anywhere else.
    htmlDoc.getElementById("ext-gen42").Click
    Application.Wait (Now + TimeSerial(0, 0, 2))
    htmlDoc.getElementById("ext-gen44").Click
    Application.Wait (Now + TimeSerial(0, 0, 2))
    htmlDoc.getElementById("ext-gen46").Click
    Application.Wait (Now + TimeSerial(0, 0, 2))
    htmlDoc.getElementById("ext-gen42").Click   '##### NG ######
End Sub

所以我的问题是识别“ext-gen42”元素的正确方法是什么,以便我可以单击或按下它来显示该选项卡,以便我可以从中删除信息?。 我确定我会 运行 进入更多的“css”元素(或任何它们),因此我将不胜感激任何帮助。

如果点击功能不起作用,您可以尝试更改选项卡的 class 和选项卡内容的 class 以激活 位置报告 选项卡。

您可以更改 位置报告 选项卡 class 以激活并隐藏 图例 选项卡内容。请尝试将不起作用的“点击”部分替换为:

doc.getElementById("ext-comp-1065__ext-comp-1004").setAttribute "class", ""
doc.getElementById("ext-comp-1065__locationReport").setAttribute "class", "x-tab-strip-active"
doc.getElementById("ext-comp-1004").setAttribute "class", "x-panel x-panel-noborder x-hide-display"
doc.getElementById("locationReport").setAttribute "class", "x-panel x-panel-noborder"

您可以在 IE 中查看 result