Bash script to click button on an HTML page

I'm trying to write a Bash script that opens a browser, clicks an HTML button on a webpage, and closes the browser after getting response. I need this script to be executable from within Terminal. If not a Bash script, then maybe a Python script.

Form is:

<form method="post" action="duophones?yc=sasv1S2f8mXKITRJS1XrenZ30SA-" enctype="application/x-www-form-urlencoded"> 
<input name="phone" value="+15551231235" type="hidden">
<input name="action" value="dapact" type="hidden">
<input name="session" value="0" type="hidden">
<button class="c01 c01v0" name="action" type="submit" value="Activate +15551231234 over SMS (recommended)" override="true">Activate +15551231234 over SMS (recommended)</button> 
</form>

Script I have is:

curl -d value="Activate +15551231234 over SMS (recommended)" www.testpage.com

Any help or direction would be appreciated!


Solution 1:

You should consider using opensource Selenium.

With a Selenium IDE plugin to Firefox you can record your actions (opening a page, clicking on a button), replay these songs, and generate Python or Ruby scripts rich you can customise and call from CLI.

Sample script testing default Apache availability might look like this:

#!/usr/local/bin/python

DEST_URL='http://webserver.dev/'

import unittest
from selenium import webdriver
from selenium.webdriver.common.proxy import *

class WebInterfaceTest(unittest.TestCase):

  def setUp(self):
     self.driver = webdriver.Firefox()

  def tearDown(self):
     self.driver.quit()

  def test_web_interface(self):
    self.driver.get(DEST_URL)
    self.assertIn('Apache2 Debian Default Page: It works', self.driver.title)

if __name__ == "__main__":
  unittest.main()

Solution 2:

Use cURL (curl). It's a command line tool that transfers data using various protocols including HTTP. The beauty here is that you don't need a GUI browser; everything will be handled at the command line level.

The "button" you want to press will be located in some sort of form. You can use the Developer Tools in Chrome or Firebug in Firefox to see the code for the form. Following is an example form with a single text box and a submit button:

<form action="http://foo.bar/helloworld.html" method="POST">
<input type="text" name="phone"> <br/>
<input type="submit">
</form>

If you were to fill out this form in your web browser and click submit, you would basically send whatever you typed in the text box and sent the request to the server.

The important thing to notice here is that the form creates a text "field" called phone which is where it will store the data that you enter to be posted. In this case, when you type your phone number (+12125551212) it assigns that value to phone. When you hit the Submit button it sends phone="+12125551212 as part of the POST.

So, using what I am assuming is the phone number you want to submit as the value for the variable, in curl, the code would look like this:

curl -d "phone=+12125551212" http://foo.bar/helloworld.html

Since curl is a command line utility, you can put it natively into a bash script:

#/usr/bin/bash

curl -d "phone=+12125551212" http://foo.bar/helloworld.html

exit