0

This is part 2 from my original post here. So now once logged in, I have to make two clicks to display the information I'm ultimately trying to scrape. I cannot seem to figure out the proper way to drill down to get to the clicks to work. It is all buried within a form. The 1st image shows the form structure. The 2nd image shows all of the code where I'm trying to get to the Current Situation link below the Supervisor span. I had to use the following (as you see from my previous post) to get IE to stay connected so not sure if that has any impact:

Dim objIE As Object

Set objIE = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")

In an attempt to automate the Current Situation tab click, I've tried the following with no luck (with and without the 0)

objIE.document.getElementById("1617")(0).Click

I'm more interested in getting educated in addition to getting an answer. Is there a method to drilling down into information within a form? I was under the impression that ALL the elements of a webpage were pulled in during loading. So is this not truly an element in my minimal understanding with webpage automation?

As a note, I do have to click Supervisor to get the tree below it to display. Thanks in advance for your help!

UPDATE: Ok a major oversight here I think. Based on my previous post, the login functions perfectly. But once logged in, a new window gets created. So I believe that is the challenge, right? It can't find any of these IDs or Elements because it's looking at the original IE object, not the new window. So how do I get it to activate/access the new window? Sorry I missed that earlier!

1 Answers1

1

Method getElementById returns a single element, not a collection, unlike for example getElementsByClassName (look at "Element" vs "Elements" in method name). Therefore (0) or any other index of collection should not be used.

Correct syntax is:

objIE.document.getElementById("1617").Click

For pop-up windows try this:

Dim wURL As String
Dim myWindow As Object

For Each myWindow In CreateObject("Shell.Application").Windows

    wURL = myWindow.LocationURL

    If InStr(wURL, "constant_part_of_popup_window_link") <> 0 Then
         'do stuff
         myWindow.Quit
         Exit For
    End if

Next myWindow

For accessing an element within iFrame:

myWindow.document.getElementById("frameMainMenu").contentWindow.document.getElementById("1617").Click
Ryszard Jędraszyk
  • 2,296
  • 4
  • 23
  • 52
  • Thanks Ryszard. When I try this, I keep getting the error "Run-time error '91': Object variable or With block variable not set. – user9837831 May 28 '18 at 12:16
  • It means that there is no element with ID "1617" on the page. – Ryszard Jędraszyk May 28 '18 at 12:32
  • How do I click the word supervisor? `` `Supervisor` Is this where I would use innertext? – user9837831 May 28 '18 at 12:47
  • There is no ID in this HTML. Try `objIE.document.getElementsByClassName("branch")(0).Click`. If there are some elements with class "branch" before this element, 0 will need to be replaced with proper index. – Ryszard Jędraszyk May 28 '18 at 13:36
  • Please see my update. Once logged in, a new window is created and all of what I am attempting to do is on that window now, not the original window. – user9837831 May 28 '18 at 14:48
  • I updated my answer with how-to find and operate on IE pop-up windows. Hopefully it will work considering previous problem with disconnecting standard IE instance. – Ryszard Jędraszyk May 28 '18 at 15:20
  • Ok I tried this and the InStr cycled through but never achieved anything other than 0. Could I select/activate the new window based on its title/name? – user9837831 May 28 '18 at 15:37
  • The code which I posted operates based on name of window, which is either path or website address. Replace "constant_part_of_popup_window_link" with whole or part of your pop-up window link. – Ryszard Jędraszyk May 28 '18 at 15:48
  • Perfect! So the new pop-up does not have an address bar but I took some screenshots while it loaded and was able to identify a unique portion of the address as compared to the initial address on the original window. So using `If InStr(wURL, "NoSSL") <> 0 Then` followed by `myWindow.Quit` it does actually close out the new pop-up. So the identification is correct. But now if I replace the `myWindowQuit` with `objIE.document.getElementsById("1617").Click` it gives be the same error as before: "Run-time error '91': Object variable or With block variable not set. – user9837831 May 28 '18 at 16:56
  • You meant `objIE.document.getElementById("1617").Click`. I know that you used correct method because of an error you get. And the reason is that despite getting pop-up window object, you still try to operate on "old" IE window. Use `myWindow` from my example code instead of `objIE`. – Ryszard Jędraszyk May 28 '18 at 17:05
  • Sorry for that typo. `myWindow.document.getElementById("1617").Click` results in the same error. – user9837831 May 28 '18 at 17:13
  • Try `SaveResponse` from my answer here: https://stackoverflow.com/questions/50296186/ie-in-vba-code-lose-connection/50302416#50302416 Just below the code is a syntax how to use it. You will be able to open html document on your desktop then and see with developer tools if there is any element with ID "1617". This ID should not exist, considering error which you received, but you will be able to check how the element is marked. – Ryszard Jędraszyk May 28 '18 at 17:35
  • Ok I ran it before and within the For loop. When running before, it pulls info that matches what I see from the original page when using Shift+F12. The results when running inside the For loop as `SaveResponse myWindow.document.body.innerHTML` had very little results: `` which is much less than I see with Shift+F12 – user9837831 May 28 '18 at 18:39
  • I added in `myWindow.document.getElementById("frameMainMenu").Click` which did not throw an error but not sure this is really a click. It seems like I need to tree my way through all the way down somehow? – user9837831 May 28 '18 at 18:51
  • What element do you try to click on and how are you sure that it is marked with ID "1617"? Is this element visible in HTML file created by 'SaveResponse`? Is there any element with ID "1617" at all when you use `SaveResponse myWindow.document.body.innerHTML` and check it with developer tools? – Ryszard Jędraszyk May 28 '18 at 18:58
  • In the 2nd image, that is the entirety of the code I see from the new window. But when I run the SaveResponse, the only thing pulled from the window is `` There's nothing else in the file. I see the ` – user9837831 May 28 '18 at 23:11
  • Ryszard - appreciate the patience and support! Works perfectly. – user9837831 May 29 '18 at 15:07