0

I am trying to click on an HTML drop-down menu in IE from VBA. Right now, I'm actually using Sendkeys "{Tab}" over 21 times to get to the element, and then I Sendkeys "{Enter}" to get the drop-down. Obviously, this is a horrible solution, but I can't seem to get anything else to work

Here is the HTML code of the element I want to click:

<tr>
<td height='21'></td>
<td colspan='5'  valign='top' align='left'>
<DIV id='win0div$ICField28$0'><table cellpadding='0' cellspacing='0' cols='1'  class = ' ' id='$ICField28$scrolli$0' width='948'>
<tr><td><DIV id='win0divGP$ICField28$0'><table cellspacing='0' cellpadding='0' border='0' width = '100%'  class='PSLEVEL1SCROLLAREAHEADER'  style = 'border:0'><tr><td  class='PSLEVEL1SCROLLAREAHEADER'  align='left' id = 'PSCENTER'><table class='PSRIGHTCORNER' cellspacing='0' cellpadding='0' border='0' style ='height:100%;' width='100%' ><tr><td  class='PSLEVEL1SCROLLAREAHEADER PSLEFTCORNER'  style = 'border:0;padding-left:0px;' align='left' ><a name='$ICField28$expand$0' id='$ICField28$expand$0' tabindex='71' href="javascript:submitAction_win0(document.win0,'$ICField28$expand$0');"><img src='/cs/fsprd/cache/PT_EXPAND_1.gif' alt='Expand section Prepayment Penalty' title='Expand section' border='0' /></a>&nbsp;Prepayment Penalty&nbsp;</td>
</tr></table></td></tr></table></DIV></td></tr>

I've tried doing many things to click the HTML element such as:

Dim IE as object
Set IE = CreateObject("InternetExplorer.Application")
IE.document.getelementsbytagname("img")(0).click

but have had no luck.

Does anyone have any ideas of what I could do to click this drop-down? Please let me know if I can provide more information.

2 Answers 2

1

Here is another approach. Basically it populates a collection of img tags, then from there iterates over each one looking for when the src matches.

Option Explicit

Sub findElementBySrc()
    Dim IE          As Object
    Dim element     As Object
    Dim elements    As Object

    Set IE = CreateObject("InternetExplorer.Application")

    'Find all the img Tags, this is in a collection
    Set elements = IE.document.getElementsByTagName("img")

    'iterate over the collection to find an item -
    'that matches the src property
    For Each element In elements
        On Error Resume Next ' to skip over elements without a src property
        If element.src = "/cs/fsprd/cache/PT_EXPAND_1.gif" Then
            element.Focus
            element.Click
            'element.FireEvent ("OnClick") 'commented out, sometimes needed
            Exit For
        End If
    Next

    Set IE = Nothing
End Sub
Sign up to request clarification or add additional context in comments.

6 Comments

This makes sense, but for some reason is not working... Ugh
We may need to the see the HTML. Perhaps the element is in a frame, etc. Hard to say without seeing the page code.
I can almost guarantee the element is in a frame. How would I capture it in such a case? I'll try and pull the code
Use the element inspector provided with IE. you should see the path in the DOM to get to the elect. You should see frame or iframe along the way.
In this case, do I need to select the frame in some way in order to be able to select the elements? What would this look like?
|
0

Try this:

Dim IE As Object
Dim img As HTMLImg 
Dim i As Integer 
Set IE = CreateObject("internetexplorer.application")

IE.navigate "yourwebsite"

Set HTMLdoc = IE.Document 
Set img = Nothing 
i = 0 
While i < HTMLdoc.images.Length And img Is Nothing 
    If HTMLdoc.images(i).alt = "Expand section Prepayment Penalty" Then Set img = HTMLdoc.images(i) 
    i = i + 1 
Wend 

If Not img Is Nothing Then 
    img.parentElement.Focus 
    img.parentElement.click 
Else 
    MsgBox "Image title not found" 
End If 

Try this too:

Dim IE As Object 
Dim i As Integer 
Set IE = CreateObject("internetexplorer.application")

IE.navigate "yourwebsite"

Set HTMLdoc = IE.Document 

For each l in HTMLdoc.getElementsByTagName("a") 
   If l.ClassName = "PSLEVEL1SCROLLAREAHEADER PSLEFTCORNER" Then
       l.Click
       Exit For
   End if
Next

14 Comments

Try updated post. Make sure to set reference to Microsoft HTML Object Library.
comment out 'Dim HTMLdoc As MSHTML.HTMLDocument
when you hoover over HTMLdoc.images, does it return anything? does the image not have an id or name associated with it?
no, don't think so, it works fine for me, need to see the webpage screen shot code of the image. I also changed the HTMLdoc.images(i).Title to HTMLdoc.images(i).title since your image code said "title" see if that helps.
Are there other images that has the same title "Expand section" ? I have a feeling there are 5 other images on your page that has the same title therefore, when the code sets the img, it's setting on the wrong image which is the last img with the title "Expand section" and therefore not clicking on the right image.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.