Kathir vtv
Kathir vtv

Reputation: 425

Automate IE with VBA - Click anchor button with span

I am currently doing automation for uploading file in saleforce.

The HTML text is:

 <div>
<a href="javascript:void(0);" onblur="chatter.getFeed().contentCommentActionBlur(this);" onfocus="chatter.getFeed().contentCommentActionFocus(this);" onclick="chatter.getFeed().createContentComment(this,'0D52700002cc67d', false, true);" onmousedown="chatter.getFeed().contentCommentActionMouseDown(this);" class="cxcontentcommentaction">
<img class="cxcontentcommentactionimg" src="/s.gif">
<span class="cxcontentcommentactiontext">Attach File</span>
</a>
</div>

For click the anchor user need to go down and press "Attach file" button.

I have tried with two different doing

1.Coding get element by anchor class="cxcontentcommentaction":

Set htmldoc = mydoc.getElementsByClassName("cxcontentcommentaction")
    htmldoc.Click

2.Coding get element by span class="cxcontentcommentactiontext":

Set htmldoc = mydoc.getElementsByClassName("cxcontentcommentactiontext")
    htmldoc.Click

For both I am getting error "Object doesn't support this property

After your comment, i have tried like this,

Set oElementCollection = mydoc.getElementsByClassName("cxcontentcommentactiontext")

    For Each link In oElementCollection
    If link.innerHTML = "Attach File" Then
        link.Click
        Exit For
    End If
Next link

I have tried for below HTML

<a href="javascript:window.invokeOnClickJS_00bG0000000ikPK%28this%29" class="menuButtonMenuLink">
Send an Email</a>

Below code is working properly for above html

Set oElementCollection = mydoc.getElementsByClassName("menuButtonMenuLink")


    For Each link In oElementCollection
    If link.innerHTML = "Send an Email" Then
        link.Click
        Exit For
    End If
Next link

But with "Attach File" button which mentioned in this question only getting problem.

Upvotes: 1

Views: 11373

Answers (1)

Axel Richter
Axel Richter

Reputation: 61852

The method getElementsByClassName returns a collection of elements with this class name. This is because a HTML DOM can contain multiple elements with the same class name. That's why the plural form getElements. So you need to know which element from the elements collection you need. If it is the first, then:

...
Set oElementCollection = mydoc.getElementsByClassName("cxcontentcommentaction")
oElementCollection(0).Click
...

If the first is not the right, then try

For i = 0 To oElementCollection.Length - 1
 MsgBox oElementCollection(i).innerHTML
Next

to find the right one.

Edit:

After your additions in the question:

with

Set oElementCollection = mydoc.getElementsByClassName("cxcontentcommentactiontext") 

you are probably getting a collection of span objects not links. But those span elements should be within links. So the links are their parents.

So

Set oElementCollection = mydoc.getElementsByClassName("cxcontentcommentactiontext")

    For Each span In oElementCollection
    If span.innerHTML = "Attach File" Then
        span.parentNode.Click
        Exit For
    End If
Next span

Upvotes: 4

Related Questions