Me.EdgeBrowser0.ExecuteJavascript click a button

wackywoo105

Registered User.
Local time
, 17:03
Joined
Mar 14, 2014
Messages
203
Can anyone help with how to click this button?

It has no normal ID and I can't find it's position searching for the inner text.

Code:
<input class="ipad-radio-btn-margin btn btn-default btn-largesize margin-right-5" data-ajax-url="/OPH/OphthalmicGosFour/SubmitSupplierDeclaration" data-btn-save="" data-image-source-id="sigCanvas" data-source-id="DivContainer" data-target-id="DivContainer" name="btnSave" type="button" value="Save for Later">
 
Did you try document.getElementsByName('btnSave')[0] In the browser? and if it works there, from VBA
document.getElementsByName('btnSave')(0)

You can change the 0 in case it finds more with that name and you can also query by another thousand methods.
 
I did that that but couldn't find it. I will write a loop and see if that brings it up.
 
I've used several variation on the following:

Code:
For x = 0 To 50
Debug.Print x & " - " & Me.EdgeBrowser0.RetrieveJavascriptValue("document.getElementsByTagName(" & Chr(34) & "button" & Chr(34) & ")[" & x & "].innerText")
Debug.Print x & " - " & Me.EdgeBrowser0.RetrieveJavascriptValue("document.getElementsByTagName(" & Chr(34) & "btnSave" & Chr(34) & ")[" & x & "].innerText")
DoEvents
Next x

But cannot find anything from the button I need to press. Alongside other buttons I can see on screen some are blank and other just show "x". I was doing this in the console but the text inside the button never came up.

Is there another way to activate it? Could I add an id to the element to allow me to click it?

I'm still also struggling with unlocking some text boxes. I had it working but it was glitchy. How do you fire the onblur event on a text box?
 
Code:
Debug.Print x & " - " & Me.EdgeBrowser0.RetrieveJavascriptValue("document.getElementsByName(" & Chr(34) & "btnSave" & Chr(34) & ")[" & x & "].value")

Darn. I needed to look for value. Sorry it's been a long day.

Sadly:

Code:
Me.EdgeBrowser0.ExecuteJavascript "document.getElementByName(" & Chr(34) & "btnSave" & Chr(34) & ")[2].click();"

Doesn't work.
 
Last edited:
Please take a look at the element you're trying to find

It's an input tag, which means it's between angle brackets <input>.
It also has a name attribute as "btnSave".

Look at it:
HTML:
<input class="ipad-radio-btn-margin btn btn-default btn-largesize margin-right-5" data-ajax-url="/OPH/OphthalmicGosFour/SubmitSupplierDeclaration" data-btn-save="" data-image-source-id="sigCanvas" data-source-id="DivContainer" data-target-id="DivContainer" name="btnSave" type="button" value="Save for Later">

But you're using getElementsByTagName trying to find a button, but it's an input
Debug.Print x & " - " & Me.EdgeBrowser0.RetrieveJavascriptValue("document.getElementsByTagName(" & Chr(34) & "button" & Chr(34) & ")[" & x & "].innerText")

And you're trying to use getElementsByTagName with btnSave, but, again, it's an input
Debug.Print x & " - " & Me.EdgeBrowser0.RetrieveJavascriptValue("document.getElementsByTagName(" & Chr(34) & "btnSave" & Chr(34) & ")[" & x & "].innerText")

Try this:
Code:
Debug.Print x & " - " & Me.EdgeBrowser0.RetrieveJavascriptValue("document.getElementsByName(" & Chr(34) & "btnSave" & Chr(34) & ")[" & x & "].innerText")

I'm using getElementsByName, not getElementsByTagName
 
Please take a look at the element you're trying to find

It's an input tag, which means it's between angle brackets <input>.
It also has a name attribute as "btnSave".

Look at it:
HTML:
<input class="ipad-radio-btn-margin btn btn-default btn-largesize margin-right-5" data-ajax-url="/OPH/OphthalmicGosFour/SubmitSupplierDeclaration" data-btn-save="" data-image-source-id="sigCanvas" data-source-id="DivContainer" data-target-id="DivContainer" name="btnSave" type="button" value="Save for Later">

But you're using getElementsByTagName trying to find a button, but it's an input


And you're trying to use getElementsByTagName with btnSave, but, again, it's an input


Try this:
Code:
Debug.Print x & " - " & Me.EdgeBrowser0.RetrieveJavascriptValue("document.getElementsByName(" & Chr(34) & "btnSave" & Chr(34) & ")[" & x & "].innerText")

I'm using getElementsByName, not getElementsByTagName
Thanks. Output is:

0 -
1 - Cancel Claim
2 -
3 - RetrieveJavascriptValue timed out. Please verify the JavaScript expression supplied is valid.
4 - RetrieveJavascriptValue timed out. Please verify the JavaScript expression supplied is valid.
5 - RetrieveJavascriptValue timed out. Please verify the JavaScript expression supplied is valid.
6 - RetrieveJavascriptValue timed out. Please verify the JavaScript expression supplied is valid.
7 - RetrieveJavascriptValue timed out. Please verify the JavaScript expression supplied is valid.
8 - RetrieveJavascriptValue timed out. Please verify the JavaScript expression supplied is valid.
9 - RetrieveJavascriptValue timed out. Please verify the JavaScript expression supplied is valid.
10 - RetrieveJavascriptValue timed out. Please verify the JavaScript expression supplied is valid.
etc.

I want to be able to trigger or click the "save for later" and "save and next" buttons. They are clickable on screen but I cannot figure out how to action them using javascript

Code:
<div class="col-xs-8 col-sm-10 text-right">
            <button class="ipad-radio-btn-margin btn btn-default btn-largesize margin-right-5" id="btnRevert" type="button">Revert to Draft</button>
                <button class="ipad-radio-btn-margin btn btn-default btn-largesize margin-right-5" id="btnCancelClaim" name="btnSave" value="Cancel" type="button">Cancel Claim</button>
            <input class="ipad-radio-btn-margin btn btn-default btn-largesize margin-right-5" data-ajax-url="/OPH/OphthalmicGosFour/SubmitSupplierDeclaration" data-btn-save="" data-image-source-id="sigCanvas" data-source-id="DivContainer" data-target-id="DivContainer" name="btnSave" type="button" value="Save for Later">
            <input class=" ipad-radio-btn-margin btn btn-default btn-success btn-largesize margin-right-5" data-ajax-url="/OPH/OphthalmicGosFour/SubmitSupplierDeclaration" data-btn-validate="" data-image-source-id="sigCanvas" data-read-id="False" data-source-id="DivContainer" data-source-name="SupplierDeclaration" data-success-url="/OPH/OphthalmicGosFour/ReceiverDeclaration" data-target-id="DivContainer" data-target-name="PatientsDeclaration2" id="btnSaveNext" name="btnSaveNext" type="button" value="Save and Next">
        </div>
 
Really it's time to look at the API documentation you mentioned!!

I want to be able to trigger or click the "save for later" and "save and next" buttons.
Try:
Code:
' Save for later:
Me.EdgeBrowser0.ExecuteJavascript "document.QuerySelector('input[type=" & Chr(34) & "button" & Chr(34) & "][value=" & Chr(34) & "Save for Later" & Chr(34) & "']).submit();"
' Save and next:
Me.EdgeBrowser0.ExecuteJavascript "document.QuerySelector('input[type=" & Chr(34) & "button" & Chr(34) & "][value=" & Chr(34) & "Save and Next" & Chr(34) & "']).submit();"
Nb, the values might be case sensitive to the value attribute in the actual buttons
 
<input class="ipad-radio-btn-margin btn btn-default btn-largesize margin-right-5" data-ajax-url="/OPH/OphthalmicGosFour/SubmitSupplierDeclaration" data-btn-save="" data-image-source-id="sigCanvas" data-source-id="DivContainer" data-target-id="DivContainer" name="btnSave" type="button" value="Save for Later">
This one can be clicked with the following example. Notice that this query method returns an array, so you need square brackets. If it does not return what you want with 0, then see what number it is.
Me.EdgeBrowser0.ExecuteJavascript "document.getElementsByName('btnSave')[0].click"

<input class=" ipad-radio-btn-margin btn btn-default btn-success btn-largesize margin-right-5" data-ajax-url="/OPH/OphthalmicGosFour/SubmitSupplierDeclaration" data-btn-validate="" data-image-source-id="sigCanvas" data-read-id="False" data-source-id="DivContainer" data-source-name="SupplierDeclaration" data-success-url="/OPH/OphthalmicGosFour/ReceiverDeclaration" data-target-id="DivContainer" data-target-name="PatientsDeclaration2" id="btnSaveNext" name="btnSaveNext" type="button" value="Save and Next">
This one has an id attribute, so you can use that instead
Me.EdgeBrowser0.ExecuteJavascript "document.getElementById('btnSaveNext').click"

Thanks. Output is:

0 -
1 - Cancel Claim
2 -
3 - RetrieveJavascriptValue timed out. Please verify the JavaScript expression supplied is valid.
...
Here's a little piece of advice from someone who did what you did at some point: I think you really need to stop trying to find your references from VBA with loops. Do your research from the actual browser using its console, do it manually, trial and error is much faster in this context than trying to find the right setting for the loop. Besides, the browser's console gives you visual feedback of what you're selecting and it has all the tools you need. Once you find a reference, then you can adapt it to VBA to create your automated scraping VBA program. It would take mere seconds to find any element in any website using just the browser, if you keep trying to find references using just VBA, you're gonna make this super difficult for yourself.
 
Many thanks for your help and patients. As you can probably tell I'm learning as I go along. I would like to use the API in the future, and will build myself up to that. ATM I am under time pressure to get something working before 1st Jan. If I can get it working reliably with the internet portal I will be sure the back end is to being used with the API. The internet portal also allows me to put interrupts in the code, so I can check what has been entered and make sure it's correct before submission.

Are there any online resources that would help me with building up to using API as I've never done it before?

I tried those examples in VBA and console and get the following:

Code:
GetSignatureDocument:1
Failed to load resource: the server responded with a status of 500 ()
document.getElementById('btnSaveNext').click
ƒ click() { [native code] }
document.getElementsByName('btnSave')[0].click
ƒ click() { [native code] }
document.getElementsByName('btnSave')[1].click
ƒ click() { [native code] }
document.getElementsByName('btnSave')[2].click
ƒ click() { [native code] }
document.getElementsByName('btnSave')[3].click
VM1006:1 Uncaught TypeError: Cannot read properties of undefined (reading 'click')
    at <anonymous>:1:41
(anonymous) @ VM1006:1
document.getElementsByName('btnSave')[4].click
VM1011:1 Uncaught TypeError: Cannot read properties of undefined (reading 'click')
    at <anonymous>:1:41
(anonymous) @ VM1011:1
document.getElementsByName('btnSave')[5].click
VM1015:1 Uncaught TypeError: Cannot read properties of undefined (reading 'click')
    at <anonymous>:1:41
(anonymous) @ VM1015:1
 
Last edited:
Well, it appears there are 3 elements with the name btnSave there. From 0 to 2. Since you're trying to access elements 3 through 5 as well and it returns that error with them, it means they don't exist. If you're seeing more buttons of that kind, they don't go by the name "btnSave". So you need to inspect the website further.

Did it click the elements that do exist?
 
Did you try the variation I posted in Post #9 ?
I think I've tried everything, but will double check tomorrow.


On another note, how do I check to see if a checkbox is selected or not?

I've tried isSelected(), but it just errors and .value always returns true.
 
On another note, how do I check to see if a checkbox is selected or not?
You need a way to identify the checkbox.
If it has a name attribute, then:
Code:
Debug.Print Me.EdgeBrowser0.RetrieveJavascriptValue("document.querySelector('input[name=" & Chr(34) & "NameOfCheckbox" & Chr(34) & "]').checked")

If it has an ID:
Code:
Debug.Print Me.EdgeBrowser0.RetrieveJavascriptValue("document.getElementById('IDOfCheckbox').checked")
 

Users who are viewing this thread

Back
Top Bottom