1

I'm using VBA and stuck on a way to trigger a Download button in HTML PAGE which inspects below codes:

<input onClick="javascript:__dopostback('GridView1','Accountnumber$0')" type="button" value="Download"></input>

More than one Download button exists on this page and I want to click on the first one.

enter image description here

In these elements the unique Attribute is:

"OnClick=javascript:__dopostback('GridView1','Accountnumber$0')"

But I don't know how to trigger with below VBA:

On Error GoTo Err_Clear
 myURL = "Enter your url"
 Set MyBrowser = New InternetExplorer
 MyBrowser.Silent = True
 MyBrowser.Navigate myURL
 MyBrowser.Visible = True
 Do
 Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE
 Set htmlDoc = MyBrowser.Document
 htmlDoc.all.txtAccNo.Value = ""
 htmlDoc.all.ddlReasons.Value = ""
 htmlDoc.all.txtRequester.Value = "" 
 htmlDoc.all.txtMobNo.Value = ""
 htmlDoc.all.txtLandLine.Value = ""
 htmlDoc.all.txtEmailAdd.Value = ""
 htmlDoc.all.ddlRelation.Value = ""
 htmlDoc.all.button1.Click

 '**OnClick event required here **

 htmlDoc.getElementsByXpath("input[type=""button""]")(0).Click  '<--(Not Working)

 Err_Clear:
 If Err <> 0 Then
 Err.Clear
 Resume Next
 End If 
3
  • getElementsByXpath isn't in my Microsoft HTML Object Library - is it in yours? Commented Apr 5, 2017 at 11:35
  • Yes I have, If you don't have HTML Object Library so what about OnClick event Commented Apr 5, 2017 at 11:42
  • MS office 2012. Commented Apr 5, 2017 at 11:48

1 Answer 1

1

Interestingly you are calling getElementsByXPath but I can't find this method in my Microsoft HTML Object Library (Win10/ Excel 2013).

Perhaps you could try using a CSS selector instead which you can use with the querySelector method of the HTMLDocument class:

enter image description here

And then, instead of using:

htmlDoc.getElementsByXpath("input[type=""button""]")(0).Click  <--(Not Working)

You could try this (untested):

Dim strSelector As String
Dim objElement As Object

strSelector = "table#GridView1 > tbody > tr > td > input[onclick=""javascript:__doPostBack('GridView1','Account_number$0')""]"

Set objElement = htmlDoc.querySelector(strSelector)
If Not objElement Is Nothing Then
    objElement.Click
Else
    MsgBox "Selector did not work!"
End If

Edit

Having had a look at the page source, and in line with the OP statement that:

More than one Download button exists on this page and I want to click on the first one.

Then we can simplify the select to just this:

table#GridView1 > tbody > tr > td > input[onclick]

Which translates to find the first <input> with an onclick attribute in the table body.

So the updated VBA is:

Dim strSelector As String
Dim objElement As Object

strSelector = "table#GridView1 > tbody > tr > td > input[onclick]"

Set objElement = htmlDoc.querySelector(strSelector)
If Not objElement Is Nothing Then
    objElement.Click
Else
    MsgBox "Selector did not work!"
End If

The selector is tested in Chrome console, so my assumption is that it should work in the VBA in a similar fashion. HTH.

enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

Giving error and highlight last instruction objElement.Click
Unfortunately that means the querySelector did not work and objElement will be Nothing. I updated the code to alert if that is the case. Do you have a public URL that I can use so I can test the code?
Yes, I have public Url: (ke.com.pk/complaints-services/duplicate-bill) But it has Captcha (Characters Image) also.
Any solution ? @Robin
That URL you referenced doesn't have a table with an id of GridView1... so I can't test. I posted the sample code to offer a solution but unfortunately there is not much more to offer unless we can see the HTML you are working with.
|

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.