Web Automation using appscript To Fill out A Form

387 views
Skip to first unread message

Le pirate noir

unread,
May 27, 2022, 3:19:58 PM5/27/22
to Google Apps Script Community
Hi,

I would like to be able to fill a web form and submit it by using appscript. 

I have started to write the code :


  var str = UrlFetchApp.fetch(url).getContentText()

   var name = "John"
  var lastname = "Duncan"
  var email = "j...@hotmail.com

  var xpathname  = "//*[@id='ci_firstname']"
  var xpathnlastame  = "//*[@id='ci_lastname']" //*[@id="ci_lastname"]
  var xpathcourriel  = "//*[@id='ci_email']"

From here, I'm blocked. I would like to access the field name by using the xpath method. 

How can i do this ?

Regards !

Alan Wells

unread,
May 27, 2022, 7:38:28 PM5/27/22
to Google Apps Script Community
You can't autofill a form on a website that you don't own and submit it with Apps Script.

Clark Lind

unread,
May 27, 2022, 8:32:46 PM5/27/22
to Google Apps Script Community
Yes, this typically can't be done unless you dissect the form and find the form action

In this case, it looks like they use a company called Cyberimpact:
<form class="ui form" action="https://app.cyberimpact.com/optin" method="post" accept-charset="utf-8">

So when the submit button is pressed, the form content is sent to that web address using a post method.
Then if you look at the rest of the form items, you can see they are looking for the first, last, and email (as you know). But they are also looking for other content, like the 
account (which I'm guessing is the company where the form resides (Bouret)). 

You can research how to do a http post method elsewhere, but if you build an object with the expected content, you should be able to use Apps script like this. 
If you define the firstName, lastName and email, you can then pass them to a function like this (untested--just theory):

function makeRequest(firstName, lastName, email) {
  var url = "https://app.cyberimpact.com/optin";
 
  var options = {
    method: 'post',
    headers['Content-Type']: 'application/json',
    ci_firstname: firstName,
    ci_lastname: lastName,
    ci_email: email,
    ci_groups: '2',
    ci_account: '12911cea-1ee8-4fa5-edfa-5b87ccd958e0',
    ci_language:'en_ca',
    ci_sent_url: '',
    ci_error_url: '',
    ci_confirm_url: ''
  };
  return JSON.parse(UrlFetchApp.fetch(url, options));  //whatever response you get will be logged in the apps script console
}

  var  firstName  = "John"
  var  lastName  = "Duncan"
  var email = "j...@hotmail.com

console.log( makeRequest(firstName, lastName, email) )

Anyway, some mix of the above is how I would go about doing it. Much of this I learned watching this one video from Coding Garden:
https://www.youtube.com/watch?v=QcaO1a-x0aQ  where he orders a Dominos pizza with javascript. It shows how he derives all the info needed.
Reply all
Reply to author
Forward
0 new messages