0

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

John Wilson
  • 100
  • 1
  • 7
  • I think your code based on this answer from me. https://stackoverflow.com/questions/63294113/automate-ie-via-excel-to-fill-in-a-dropdown-and-continue/63299608#63299608 But you do some errors because you don't understand how the code works I think. E.g. your address field has no `compositionstart` and `compositionend` events. – Zwenn Sep 01 '20 at 09:23
  • Look at the screenshots in the linked answer and than look which events are availible for your needed elements. I havn't testet which events must be triggered to do what you want. But the tab has a `mousover` event and all tabs together have a `mousedown` event. I think you need both to switch the tab. Perhaps I find later time to have a better look. – Zwenn Sep 01 '20 at 09:24
  • Yes. I did use the code that you gave me in that other answer for a different site and I truly appreciate and learned a lot from it. I couldn't get that "boro" dropdown to work initially on this one so I tried your composition start/end and it did work. I just now replaced that with a {TAB} and it works without the comp start/end. Not sure why but I'm still learning. That "ext-gen42" was what was giving me problems with this code which is why I posted this question. Yu Zhou seems to have come up with a novel way of addressing it and gets me where I need to be and I'm thankful to him for that. – John Wilson Sep 01 '20 at 13:26

1 Answers1

1

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.

Yu Zhou
  • 11,532
  • 1
  • 8
  • 22