Help to click this button with IE.document (1 Viewer)

wackywoo105

Registered User.
Local time
Today, 05:09
Joined
Mar 14, 2014
Messages
203
Can anyone help with how to click this button using IE.document?

ClickSave.jpg
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:09
Joined
Oct 29, 2018
Messages
21,496
Looks like clicking that Save button will run the AddNewAddress() function. Perhaps you could simply call that function in your code?
 

Edgar_

Active member
Local time
Today, 07:09
Joined
Jul 8, 2023
Messages
435
> Open DevTools
> Go to Console tab
> Write this (it's JavaScript)
document.getElementsByTagName("button")[0].innerText
If it returns "Save", that's your button, if it does not, try with 1, then 2, etc., until it returns "Save".

What you're doing here is telling your browser: "Hey, I need you to give me an array of all the button tags in the document", when you add the brackets, you're specifying which element you want from the array, the innerText property is the caption. So, you're basically looking for the button with caption "Save". Assuming that's the only button with caption "Save" in that document, you'll be able to click it.

From VBA, the syntax is very similar, instead of using brackets, you use parentheses, and, instead of using the .innerText property, you'll use .click
document.getElementsByTagName("button")(1234).click, where 1234 is the number you found.
 

wackywoo105

Registered User.
Local time
Today, 05:09
Joined
Mar 14, 2014
Messages
203
Amazing. Thank you very much, that works. That button is on a pop up form. I note to get it to work I have to make the form open and then click save, yet without the form open I can still insert data? Would it be possible to get it work with the form remaining hidden?

Out of interest how would I make the AddNewAddress() function run using VBA?
 

Edgar_

Active member
Local time
Today, 07:09
Joined
Jul 8, 2023
Messages
435
I can still insert data
That modal is part of the document, when it shows, it's because its hidden attribute was removed, but you can input data there. Test if you can trigger the AddNewAddress() function by simply typing that in the console.

By doing that, depending on who programmed the website, you may disrupt its inner workings, but I'm not one to judge.
 

wackywoo105

Registered User.
Local time
Today, 05:09
Joined
Mar 14, 2014
Messages
203
typing it in gives:

Code:
function AddNewAddress(){if(required()){if(CheckValidPostCode('Postcode')){var finalAddresss=GetAddressModaldata();var targetID=$('#targetControlID').val();$("#"+targetID).html(finalAddresss);$('#Address').val($('#txtAddress').text()).trigger('change');$('#performerdeclaration_sightAddress').val($('#txtAddress').text());$('#PatientDeclaration_Address').val($('#txtAddress').text()).trigger('change');$('#VenueMaintainance_Address').val($('#txtAddress').text());var arrayInput=document.getElementById('AddressModal').getElementsByClassName("required");jQuery.each(arrayInput,function(){$('span[data-valmsg-for="'+this.id+'"]').addClass('hide');$("#isPresent").hide();$("#isPostcodeValid").hide();});$('#AddressModal').modal('hide');} ...

EDIT: Playing with it it does work and updates the address, even with the box hidden. How do I call it from vba?
 
Last edited:

Edgar_

Active member
Local time
Today, 07:09
Joined
Jul 8, 2023
Messages
435
Did you call it using the parentheses at the end?
 

wackywoo105

Registered User.
Local time
Today, 05:09
Joined
Mar 14, 2014
Messages
203
Did you call it using the parentheses at the end?
yes. that when I noticed it triggered the function on the hidden box.

I've tried variations of below but can't get it to work.

Code:
IE.Document.parentWindow.execScript "AddNewAddress()"
 
Last edited:

Edgar_

Active member
Local time
Today, 07:09
Joined
Jul 8, 2023
Messages
435
Maybe @theDBguy has some ideas for that, maybe:
IE.Document.parentWindow.execScript "AddNewAddress()", "JavaScript"
also
IE.Document.parentWindow.execScript "eval(AddNewAddress())", "JavaScript"

I suggest just clicking the button.
 

wackywoo105

Registered User.
Local time
Today, 05:09
Joined
Mar 14, 2014
Messages
203
Thanks again. I realised it does work. It just requires that the pop-up address window has been previously opened. If the AddNewAddress() function is called without first doing so, it does nothing. After opening and then closing the window (having previously populated it without opening it), calling the function updates the address.

Is there any way to make the page think the window has been previously opened?
 

wackywoo105

Registered User.
Local time
Today, 05:09
Joined
Mar 14, 2014
Messages
203
this is the button that opens the pop up address box

Code:
<button class="btn btn-info ipad-Address-btn-margin" type="button" data-toggle="modal" data-target="#AddressModal" data-book-id="txtAddress">Please enter the address manually</button>

I'm not really sure how to find out what changes when it is opened and closed?

TBF just opening and closing it straight away doesn't really matter. It's not the cleanest, and I would like to understand what is going on, but as long as it works that's the main thing.
 
Last edited:

Edgar_

Active member
Local time
Today, 07:09
Joined
Jul 8, 2023
Messages
435
this is the button that opens the pop up address box

Code:
<button class="btn btn-info ipad-Address-btn-margin" type="button" data-toggle="modal" data-target="#AddressModal" data-book-id="txtAddress">Please enter the address manually</button>

I'm not really sure how to find out what changes when it is opened and closed?

TBF just opening and closing it straight away doesn't really matter. It's not the cleanest, and I would like to understand what is going on, but as long as it works that's the main thing.
The part that says data-target="#AddressModal" means there's an element in your document with Id "AddressModal". It's probably a div tag that's hidden at first. When you click the button, this box becomes visible, likely because it changes or removes its hidden attribute.

This whole thing seems to be managed by Bootstrap, a famous set of tools for making websites look good and work well. Even if you don't use Bootstrap yourself, it's good to know that it changes the class attribute of html tags within the document. In this case, the div holding the modal probably switches classes, making the modal appear. Make the window visible and invisible while Inspecting the div with Id "AddressModal" to know what changes and what to address to mimic the behavior from VBA.
 

Users who are viewing this thread

Top Bottom