Hello StackOverflow Community,
I have a question about using Access VBA to control IE.
Essentially, I'm trying to write code that will open a specific web page using IE, will look for that page for a specific link (the name of the target link will depend on the circumstances of the user), go to the new page programmatically by clicking on that link, and then repeating the process. looking for a specific link / element on a new page.
The display text for the final destination link will always be the same, but the page on which it is located will be different in each case.
My problem is to programmatically search for a second level page for the elements that are there ... my results continue to give me only the elements from the first page, even after the browser has loaded a new link.
Sorry if I do a poor job describing the context of the question so far.
My code essentially looks like this:
Dim ie As Object, ieDoc As HTMLDocument
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
strHTML = "http://targetsite.com/first-level_page"
ie.navigate strHTML
'wait for browser
While ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
'define first-level target link and search for it and then "click" it
Dim i As Integer
Dim txt As String, link As String, p As String
Set ieDoc = ie.Document
txt = "First-Level Target"
Do Until link = txt Or i >= 1000
i = i + 1
link = ieDoc.Links(i).outerText
Loop
'I know the above loop is not exactly ideal in its current form, but it does give me a working first attempt at the functionality I'm trying to build.
ieDoc.Links(i).Click
So far so good. The above code works as intended. In all cases, it correctly moves to the desired page of the second level. When I am mistaken, I try to search this second level page for the final target element:
'wait for browser
While ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
'Search for final target element (which always has the same name and anchor text on all second-level pages)
ieDoc.getElementsByName("final-target-name").Item.Click
( .getElementsByName) , . , . , . .
debug.print, , .
, HTML- , . ieDoc , ie.document, .
, , . VBA HTML-, .
!
~ JQN