Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Click IE link based on InnerText.. perplexed! 1

Status
Not open for further replies.

KristianDude

Technical User
Mar 18, 2016
65
US
My last post was too confusing and I have figured out most of it but have this one perplexing issue... I am trying to click on a link with no tag using Internet Explorer. No new window, same window. I have to use the InnerText search as the url will change based on the 1st page zip code entered. Here is the sequence...

#1) Navigate to this address then vba enters a zip code, submits and then navigates to

#2) This is where I am trying to click on "3 Day History" with VBA but having a heck of a time!

Also, here is link info if it helps anything.. thanks!..
<a href=" Day History</a>

Code:
Dim HTMLDoc As HTMLDocument
Dim Element As HTMLLinkElement
Dim IeDoc As Object
Dim ElementCol As IHTMLElementCollection
Dim link As HTMLAnchorElement

Do
'Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE

    'find 3 day
    Set ElementCol = HTMLDoc.get("a")
    For Each link In ElementCol
        If InStr(Element.innerText, "3 Day History") = 1 Then
            link.Click
            Exit For
        End If
    Next link
 
I just tried replacing the innertext search code with a simple navigate to url and that didn't work either. Somehow I am losing control of the window. I think it is happening somewhere between clicking the link on the first page and the 2nd page loading!??
 
>Set ElementCol = HTMLDoc.get("a")

Which library are you using where you'd expect the above line of code to work?
 
My apologies.. I was using all different types of coding right there to try and get it to move, but the initial one that actually worked originally was this one (below)... but I also found that even if I just type in .navigate "google.com" it still won't do anything. I think it is disconnecting after it navigates away from the first page?.. but maybe not?.. I am definitely lost on this one

Code:
'get temps
    'find 3 day
    Dim Element As HTMLLinkElement
    Dim IeDoc As Object

    'find 3 day
    For Each Element In HTMLDoc.Links
        If InStr(Element.innerText, "3 Day History") Then
        Call Element.Click
        Exit For
        End If
    Next Element
    
Do
''Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE
 
Given that you think that it is other code that you think may be causing this code to fail, don't you think it might be useful to post that earlier code - we need to be able to see a lot more of the code that you are actually running before we can help you.
 
Don't know if this might help...

Code:
Do
''Wait till the Browser is loaded
   [b]DoEvents[/b]
Loop Until oBrowser.readyState = READYSTATE_COMPLETE

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Was trying to keep it as simple as possible but I agree.. here it is!

Code:
Sub Weather()
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Dim oHTML_Element As IHTMLElement
Dim sURL As String
Dim occ As ContentControl
Dim strToday
Dim strPath
Dim iRet As Integer
Dim strPrompt As String
Dim strTitle As String

On Error GoTo Err_Clear


sURL = "[URL unfurl="true"]http://forecast.weather.gov/zipcity.php"[/URL]

Set oBrowser = New InternetExplorer
oBrowser.Visible = True
oBrowser.Silent = True
oBrowser.navigate sURL
oBrowser.FullScreen = True


Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE

Set HTMLDoc = oBrowser.Document

'time to load
WaitSeconds (2)

HTMLDoc.all.inputstring.Value = Me.txtZip

'time to load
WaitSeconds (2)

For Each oHTML_Element In HTMLDoc.getElementsByName("Go2")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next oHTML_Element

'Do
' 'Wait till the Browser is loaded
'Loop Until oBrowser.readyState = READYSTATE_COMPLETE

'Request seconds to wait for user input case by case
Dim strUser As String
'strUser = InputBox("Default page load time is 5 seconds. Change?", "Change Default Time?", 5)
strUser = "7"

    'check directory
    strPath = ActiveDocument.Path & "\Weather"
    If Len(Dir(strPath, vbDirectory)) = 0 Then
    MkDir strPath
    End If
    
    'image path
    Set occ = ActiveDocument.SelectContentControlsByTitle("ctrlCalendar").Item(1)
    strToday = strPath & "\Weather " & Format(Date, "mm-dd-yy") & ".jpg"
    strPath = strPath & "\Weather " & Format(occ.Range.Text, "mm-dd-yy") & ".jpg"
    
    'time to load
    WaitSeconds (5)
    
    'hide browser
    oBrowser.Visible = False
    
    '***SCREENSHOT TODAY'S FORECAST
    If Len(Dir(strToday, vbDirectory)) = 0 Then
        MsgBox "    Please highlight today's forecast area"
        
        Do
        'Wait till the Browser is loaded
        Loop Until oBrowser.readyState = READYSTATE_COMPLETE
        
        'show browser
        oBrowser.Visible = True
        
            'print screen
            keybd_event VK_SNAPSHOT, 1, 0, 0 'Print Screen key down
            keybd_event VK_SNAPSHOT, 1, VK_KEYUP, 0 'Print key Up - Screenshot to Clipboard
        
        'time to capture area
        WaitSeconds (strUser)
        
        'save clipboard to file
        SaveClipboardasJPEG (strToday)
        
        'hide browser
        oBrowser.Visible = False
        
        Else
        
    End If

    
    '***CHECK FOR FILE TO IMPORT
        'If Format(Date, "mm-dd-yy") <> Format(occ.Range.Text, "mm-dd-yy") Then
            If Len(Dir(strPath, vbDirectory)) = 0 Then
                MsgBox "       There is not a screenshot available for this date" & vbCr & _
                "         Please import weather for this date manually" & vbCr & _
                "                     Now closing weather import"
                Exit Sub
                Else
                
                '***IMPORT EXISTING SCREENSHOT
                MsgBox "     A screenshot is now being imported"
                   
                'insert image to document
                Set occ = ActiveDocument.SelectContentControlsByTitle("ctrlPicture").Item(1)
                
                If occ.Type = wdContentControlPicture Then
                    If occ.Range.InlineShapes.Count > 0 Then occ.Range.InlineShapes(1).Delete
                    Dim pPicFileName As String
                    pPicFileName = strPath
                        ActiveDocument.InlineShapes.AddPicture Filename:=pPicFileName, _
                        linktofile:=True, Range:=occ.Range
                        With occ.Range.InlineShapes(1)
                        .LockAspectRatio = msoCTrue
                        .width = 540
                        End With
                    End If
                End If
         'End If
                
    'IMPORT TEMP DATA
    strPrompt = "      Would you like to import temperature data?"
    strTitle = "Import Temperature Data"
    iRet = MsgBox(strPrompt, vbYesNo, strTitle)
    If iRet = vbYes Then
    
        'clear clipboard
        ClearClipboard
        
        Do
        'Wait till the Browser is loaded
        Loop Until oBrowser.readyState = READYSTATE_COMPLETE
        
        'get temps
        'find 3 day
        Dim Element As HTMLLinkElement
        Dim IeDoc As Object
    
        'find 3 day
        For Each Element In HTMLDoc.Links
            If InStr(Element.innerText, "3 Day History") Then
            Call Element.Click
            Exit For
            End If
        Next Element
            
        Do
        ''Wait till the Browser is loaded
        Loop Until oBrowser.readyState = READYSTATE_COMPLETE
        
        'wait for data to load
        WaitSeconds (3)
        
        MsgBox "      Please highlight temperture nearest to 6am"
        
        Do
        'Wait till the Browser is loaded
        Loop Until oBrowser.readyState = READYSTATE_COMPLETE
        
        'show browser
        oBrowser.Visible = True
           
        WaitSeconds (strUser)
        
        'AM data
        SendKeys "^c", True
            
        'time for data to load
        WaitSeconds (5)
        
        oBrowser.Visible = False
        
        Set occ = ActiveDocument.SelectContentControlsByTitle("ctrlAM").Item(1)
        occ.Range.PasteAndFormat wdFormatPlainText
        
        'get PM
        MsgBox "      Please highlight temperture nearest to 2pm"
        
        Do
        'Wait till the Browser is loaded
        Loop Until oBrowser.readyState = READYSTATE_COMPLETE
        
        oBrowser.Visible = True
           
        WaitSeconds (strUser)
        
        'PM data
        SendKeys "^c", True
            
        'time for data to load
        WaitSeconds (2)
        
        oBrowser.Visible = False
        
        Set occ = ActiveDocument.SelectContentControlsByTitle("ctrlPM").Item(1)
        occ.Range.PasteAndFormat wdFormatPlainText
        
    End If
    
    'release occ
    Set occ = Nothing
    
    'close browser
    oBrowser.Quit
    
    
    MsgBox "        Weather import complete"

Err_Clear:
If Err <> 0 Then
'MsgBox "Error: " & Err.Number & ". " & Err.Description, , "modDateTime.WaitSeconds"
Err.Clear
Resume Next
End If

'close browser
oBrowser.Quit

End Sub
 
Well.. I am now using a findwindow function and able to open the internet explorer window and set focus on it but still not able to use the snippet of code to click on the link that's just right there in front of me!!.. here's what it looks like.. so still perplexed as to why this isn't finding the link and clicking?? If I step through the code (below) and hover over these elements, here is what it is showing them as...

oHTML_Element = "[objectHTMLinputelement]"
oHTML_Element.innerText = ""
InStr(oHTML_Element.innerText, "3 Day History") = 0

Code:
        For Each oHTML_Element In IeDoc.Links
            If InStr(oHTML_Element.innerText, "3 Day History") Then
            Call Element.Click
            Exit For
            End If
        Next oHTML_Element
 
I placed the strtest at the end of the code and it is showing the FIRST page still.. how do I get these to reference the newest page?.. is it something to do with child links?

Code:
        Dim Element As HTMLLinkElement
        Dim IeDoc As Object
        Set HTMLDoc = oBrowser.Document
        'find 3 day
        For Each Element In HTMLDoc.Links
            If InStr(Element.innerText, "3 Day History") Then
            Call Element.Click
            Exit For
            End If
        Next Element
        
        Dim strtest
        strtest = HTMLDoc.URL
 
Well ... Sub Weather works here (clumsily, but it works ...) in that it successfully navigates to the 3 day history
 
I figured out that I wasn't allowing enough time for the page to load. Depending on connection speed and cpu usage the code was getting ahead of the web pages. Any tips on how to make this a bit more solid? I have MacGyver this thing together, but would love to get it a bit more solid. Thanks for anything!
 
Here's a basic version of the necessary navigation. Note that there are many ways to do this.

Code:
[blue]Public Sub test()
    Dim HTMLDoc As HTMLDocument
    Dim oBrowser As InternetExplorer
    Dim oHTML_Element As IHTMLElement
    Dim sURL As String
       
    sURL = "[URL unfurl="true"]http://forecast.weather.gov/zipcity.php"[/URL]
    Set oBrowser = New InternetExplorer
    oBrowser.Visible = True
    oBrowser.Silent = True
    oBrowser.navigate sURL
    'oBrowser.FullScreen = True
    
    Do
    ' Wait till the Browser is loaded
    Loop Until oBrowser.readyState = READYSTATE_COMPLETE
    
    Set HTMLDoc = oBrowser.Document
    Do
    ' Wait till the document is loaded
    Loop Until HTMLDoc.readyState = "complete"
    
    HTMLDoc.All.inputstring.Value = "07042"
    HTMLDoc.getElementsByName("Go2")(0).Click
    
    Do
    ' Wait till the document is loaded
    Loop Until HTMLDoc.readyState = "complete"
    
    For Each oHTML_Element In HTMLDoc.getElementsByTagName("a")
        If oHTML_Element.innerText = "3 Day History" Then
            oHTML_Element.Click
            Do
            ' Wait till the document is loaded
            Loop Until HTMLDoc.readyState = "complete"
            Exit For
        End If
    Next

End Sub
[/blue]
 
Thank you for this!

Code:
    Do
    ' Wait till the document is loaded
    Loop Until HTMLDoc.readyState = "complete"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top