Link to home
Start Free TrialLog in
Avatar of pritipatel0408
pritipatel0408

asked on

Javascript to automate button click

I need javascript that clicks the button on a form.

I dont have direct url. But to get to the form, please click "Apply now" on the page http://www.indeed.com/viewjob?cmp=TELES-PROPERTIES&t=Real+Estate+Assistant&jk=8e0bc33c6838aac9&q=%2440%2C000

It takes you to a form which has the continue button. I want to automate clicking the 'Continue' button.

document.forms.item(0).submit() acts weird. This is an iframe with in iframe. Can you help?

Note: This is being done for personal work automation
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

You can't - your page / iframe relationship violates the same origin policy. Although the base domain is the same your IFrame's src is https://apply.indeed.com and your main page is http://www.indeed.com - differs on sub-domain and protocol.

http://javascript.info/tutorial/same-origin-security-policy

Your main page scripts are therefore going to be denied access to the inner iframes.

The only solution is to put script into your apply.indeed.com pages to call into the parent.
Avatar of pritipatel0408
pritipatel0408

ASKER

Thank you. I am new to javascript world. Can you provide the script that I put into apply.indeed.com to make this work?
I cannot test properly but it would be something like this

This would go inside the first iframe
// GET THE URL FOR THE CONTAINER - YOUR MAIN PAGE
// BECAUSE OF THE SECURITY RESTRICTIONS WE CAN GET THE
// REFERRER (THE PARENT) BUT NOT ACCESS THE hostname AND
// OTHER MEMBERS OF THE PARENT'S LOCATION.
// WE USE A TRICK BY CREATING AN <a> ELEMENT AND SETTING
// ITS href TO THE REFERRER URL WE CAN THEN EASILY ACCESS
// THE COMPONENTS OF THE URL. IN THIS CASE WE ARE AFTER
// THE hostname

var parenturl = document.createElement('a');
parenturl.href = document.referrer;

// CHECK TO SEE IF THE hostname OF THE PARENT URL IS www.indeed.com
// IF IT IS THEN WE WANT TO SUBMIT THE FORM
if (parenturl.hostname == 'www.indeed.com') {
 
  // GET THE FIRST iframe IN THIS PAGE
  var ifr = window.frames[0];

  // GET THE FORM BY ITS ID
  var frm = ifr.document.getElementById('apply_form');

  // SUBMIT IT
  frm.submit();
}

Open in new window


NB: I tested this in the console window and it seems to work - however the form does not submit - it returns to the same page. This can be replicated by clicking on the button. Therefore I am not certain that I have fully understood your requirement.
Thank you Julian. I shall be awarding the points for the above solution. However, I cant get the above script working yet. Please let me know if you would rather want this posted as separate question.

I have been using chrome's autofill extension http://www.tohodo.com/autofill/help-chrome.html to achieve the button click automation. You can specify a javascript in autofill (please see attachment) that would automatically run on a page you open. Would you kindly take a stab at autofill to automate the button click for me?  Please let me know if I need any more clarifications.

Sorry. I dont fully understand when you mean the form does not submit. Perhaps form returns to same page when its incomplete?

Many many thanks!! Really appreciate your hard work.
autofill.png
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
We are almost there. The snippet worked great for the original link I provided. But for the following link, I dont have luck. Its puzzling given form ID and element info remain the same.  

http://www.indeed.com/cmp/Independent-Capital-Management,-Inc./jobs/Financial-Advisor-Management-Trainee-3075d86ff75e64f2?sjdu=QwrRXKrqZ3CNX5W-O9jEvWZePZcXeI16EUz3N-7_glvdMgXNrH7iSjZbyjCI-ynra3sLgM25MQBcowli_0HyIqI2sOFSgYxOKZmx55Z5rJ4 

Any idea where I could be going wrong ? Many thanks! This helps me with saving lot of hassle filling my job applications.

I have moved this into another question
https://www.experts-exchange.com/questions/28698758/Javascript-to-automate-button-click-Part-II.html
I tried running the script on that page and it seems to work. Are you still having issues with this?
I'm working in chrome and command cd() is not supported. Can you please make a script available that  does not use cd?
http://stackoverflow.com/questions/5712187/how-do-i-cd-into-an-iframe-in-chrome-developer-tools-or-firebug-lite-in-chrome
I think this is where the solution trips up. Chrome does not support the cd command and because of the different origins restriction it is not possible to access in the inner iframes through script.

The code would have to be placed in the actual page source that is loaded into the iframe.
This is sad. Can we not work around using contentWindow  like the link mentioned (http://stackoverflow.com/questions/5712187/how-do-i-cd-into-an-iframe-in-chrome-developer-tools-or-firebug-lite-in-chrome) ?
EDIT: contentWindow does not work for cross origin case.