Search code examples
excelvbainternet-explorerie-automation

Excel IE Automation identifying a "css" element


Still plundering along scraping web sites, printing from web pages, etc. but ran into a snag I'm hoping someone can help me with. The pic below is shows the tab that I'd like to activate and I have the code to get me right there but can't activate the tab. While the last line of code will work on my desktop PC, I can't get it to work on my other PC's. I likely could juxtapose IE on the other PC's to make it work but I know that I'm just identifying the element wrong and if I did it correctly it should work all the time without having to mess with the IE settings.

A pic of the site with the "Location Report" tab is below Oasis Map Site

Using Selenium on a different browser I was able to determine that the tab was identified as below::

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

My code to get me to that point is here:

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

So my question is what is the correct way to identify that "ext-gen42" element so that I can click on or press it to have that tab display so that I can scrape the information off of it?. I'm sure I'll run into more "css" elements (or whatever they are) as I continue so any help would be greatly appreciated.

Adding html as suggested


Solution

  • If the click function can't work, you could try to change the class of the tabs and the class of the tab contents to active the Location Report tab.

    You can change the Location Report tab class to active and hide the Legend tab content. Please try to replace the not working "click" part to this:

    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"
    

    You could check the result in IE.