I think the problem has to do with iframes. I got Selenium to record and run the process on both sites. The only difference I see in the resulting Selenium code is that on the GHIN site, Selenium runs a Command: "select frame" Target: "index=0". I believe this gets to the iframe(0) frame but I have not found the VBA code to penetrate access to the element id's I need. More on this later. A less likely possibility is that the use of AJAX on the GHIN site must be handled differently? The iframe and AJAX were the most obvious differences to me on inspection of the HTML. Both sites appear to use JSCRIPT, Tables and Forms.
I lookup and capture Golf Handicap updates for a list of about 50 golfers in an Excel spreadsheet. Revised handicaps are issued twice a month. My golfers are split between 2 handicap service sites:
https://www.golfhandicapnetwork.com/DefaultLookup.aspx (Handicomp) http://www.ghin.com/lookup.aspx (GHIN)
I programmed an Excel Macro that gets the job done for Handicomp but can't get the same approach to work on the GHIN website. Here's the VBA Macro for GHIN using a suggested approach I tried to access the iframe elements (Error 424 Object required). I annotate the code with remarks to show the replacement code for the very similar Handicomp handicap service lookup web site. I exclude the code to get the lookup criteria from the spreadsheet, extract the lookup results and enter them in the spreadsheet to minimize the code and focus on my inability to getElementById on the GHIN website within an iframe. I list more specifics about my failed adjustments to gain access to the iframe elements after the CODE. This sub just looks up one common name (Smith) in the state of Arizona. I also leave InternetExplorer open to view the lookup results.
Sub HCLookupGHIN()
Dim objIE As InternetExplorer
Dim doc As HTMLDocument
Set objIE = CreateObject("InternetExplorer.application")
With objIE
.Visible = True
.navigate "http://www.ghin.com/lookup.aspx" '1
'Wait for loading
Do While objIE.Busy Or objIE.readyState <> 4: DoEvents: Loop
End With
Set doc = objIE.document
' for GHIN need to click the Name & State lookup tab
doc.frames(0).document.getElementById("__tab_ctl00_bodyMP_tcLookupModes_tpNameState").Click '2 Err 424 Object Required (Also Run-time error Access is denied)
' Find and Select State from Drop Down List
doc.frames(0).document.getElementById("ctl00_CPH0_ddlState").Click '3
Set oState = doc.frames(0).document.getElementById("ctl00_bodyMP_tcLookupModes_tpNameState_cboState") '4
For i = 1 To oState.Options.Length
If oState.Options(i).Text = State Then
oState.selectedIndex = i
Exit For
End If
Next i
doc.frames(0).document.getElementById("ctl00_bodyMP_tcLookupModes_tpNameState_tbLastName").Value = LastN '5
doc.frames(0).document.getElementById("ctl00_bodyMP_tcLookupModes_tpNameState_tbFirstName").Value = FirstN '6
doc.frames(0).document.getElementById("ctl00_bodyMP_tcLookupModes_tpNameState_btnSubmit2").Click '7
'Replacement code for Handicomp Sub. This code works for me. Replace annotated statements '1-'7
'1 .navigate "https://www.golfhandicapnetwork.com/DefaultLookup.aspx"
'2 no code required for Handicomp. Name & State input available
'3 doc.getElementById("ctl00_CPH0_ddlState").Click
'4 Set oState = doc.getElementById("ctl00_CPH0_ddlState")
'5 doc.getElementById("ctl00_CPH0_lastNameLookupTxtBox").Value = LastN
'6 doc.getElementById("ctl00_CPH0_firstNameLookupTxtBox").Value = FirstN
'7 doc.getElementById("ctl00_CPH0_submit_lookup_nam_btn").Click 'Click submit button
End Sub
I can't enter data in the default GHIN Single Player lookup either using:
doc.getElementById("ctl00_bodyMP_tcLookupModes_tpSingle_tbGHIN").Value = "0000" (or the other frames(0) and iframe ID constructs)
So, I conclude that I just haven't found the secret passage to access the iframe. It is not just a problem clicking the Name & State lookup tab.
Nor could I get to the GHIN Name & State lookup form (which is what I really need) using: doc.getElementById("__tab_ctl00_bodyMP_tcLookupModes_tpNameState").Click
I also tried the following VBA code to getElementById in iframe (0) (statement '2) using:
doc.getElementById(ghinwidget_iFrame_0).contentDocument.getElementById("__tab_ctl00_bodyMP_tcLookupModes_tpNameState").Click [Run-time error 91 object variable or with block not set]
doc.getElementById(ghinwidget_iFrame_0).document.getElementById(__tab_ctl00_bodyMP_tcLookupModes_tpNameState).Click [Run-time error 91 object variable or with block not set]
Tried a few other similar constructs to no avail. I've spent a lot of time looking for answers. Sorry if I missed it.
These two web sites appear so similar. Help resolve my ignorance. I'm a beginner with internet automation and VBA programming, but I think I'm close to automating this handicap lookup task and putting the results into my spreadsheet. Any help will be greatly appreciated by a bunch of old golfers (especially me). Hope you have the simple answer or can suggest another approach.