INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Click IE link based on InnerText.. perplexed!

Click IE link based on InnerText.. perplexed!

(OP)
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) http://forecast.weather.gov/zipcity.php
Navigate to this address then vba enters a zip code, submits and then navigates to

#2) http://forecast.weather.gov/MapClick.php?CityName=...
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="http://www.wrh.noaa.gov/mesowest/getobext.php?wfo=...">3 Day History</a>

CODE --> VBA

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 

RE: Click IE link based on InnerText.. perplexed!

(OP)
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!??

RE: Click IE link based on InnerText.. perplexed!

>Set ElementCol = HTMLDoc.get("a")

Which library are you using where you'd expect the above line of code to work?

RE: Click IE link based on InnerText.. perplexed!

(OP)
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 --> VBA

'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 

RE: Click IE link based on InnerText.. perplexed!

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.

RE: Click IE link based on InnerText.. perplexed!

Don't know if this might help...

CODE

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

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Click IE link based on InnerText.. perplexed!

(OP)
Was trying to keep it as simple as possible but I agree.. here it is!

CODE --> VBA

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 = "http://forecast.weather.gov/zipcity.php"

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 

RE: Click IE link based on InnerText.. perplexed!

(OP)
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 --> VBA

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 

RE: Click IE link based on InnerText.. perplexed!

(OP)
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 --> VBA

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 

RE: Click IE link based on InnerText.. perplexed!

Well ... Sub Weather works here (clumsily, but it works ...) in that it successfully navigates to the 3 day history

RE: Click IE link based on InnerText.. perplexed!

(OP)
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!

RE: Click IE link based on InnerText.. perplexed!

Here's a basic version of the necessary navigation. Note that there are many ways to do this.

CODE

Public Sub test()
    Dim HTMLDoc As HTMLDocument
    Dim oBrowser As InternetExplorer
    Dim oHTML_Element As IHTMLElement
    Dim sURL As String
       
    sURL = "http://forecast.weather.gov/zipcity.php"
    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
 

RE: Click IE link based on InnerText.. perplexed!

(OP)
Thank you for this!

CODE --> VBA

Do
    ' Wait till the document is loaded
    Loop Until HTMLDoc.readyState = "complete" 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close