3

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!

  • 1
    What URL and button on the page do you want to click? – user3439894 Jun 28 '16 at 0:02
  • Script still not correct. Any Ideas? @allan – MacMania Jun 30 '16 at 5:36
  • That script is not correct. There is no variable named "value". – Allan Jun 30 '16 at 11:23
  • I wonder what if I switch the content of the question up, if I can get a answer. How do I use a curl command to click the Google Search button on www.google.com. I ultimately want this to be in a bast script. Once i can understand an easier example, I can replicate and apply it to my original question. @allan – MacMania Jun 1 '17 at 6:29
  • You never addressed my comment from a year ago regarding the variable named "value." Before you can understand any answer posted here, you need to get a basic understanding of HTML forms. Please review: w3schools.com/html/html_forms.asp – Allan Jun 1 '17 at 11:44
3

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()
  • I'll think of a better script to illustrate your example. – techraf Jun 28 '16 at 0:28
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
  • 1
    Advice: 1) When adding additional info edit the original question and add it there. 2) Don't use personal info like phone numbers - obfuscate them (i.e. 212-555-1234). 3) your script doesn't look anything like my example - where's the website address where the form is located, and where's the variable assignment? – Allan Jun 29 '16 at 12:15
0

Another option for robust automated web browsing is perl's WWW::Mechanize module.

To report spam to the spamcop service, I use a combination of AppleScript and perl. AppleScript interacts with Mail and gets the spam contents and the perl side handles the web browsing.

Below is an example of WWW::Mechanize to submit a form to a web server, find a specific button in the response, and to click the button:

#!/usr/bin/perl

use WWW::Mechanize;

my $spamcop_url = 'http://www.spamcop.net';

my $mech = WWW::Mechanize->new();
$mech->get( $spamcop_url );

$mech->submit_form(
        form_number => 1,
        fields      => {
            username    => 'ENTER YOUR SPAMCOP USER NAME HERE',
            password    => 'ENTER YOUR SPAMCOP PASSWORD HERE',
        }
    ) ."\n";

my $stop = 0;
while(not $stop) {

    $mech->follow_link( text => 'Report Now' ) ."\n";

    my $form = $mech->form_name( 'sendreport' );
    if ($form) {
        print "Send Report form found: ".$mech->value('reports')."\n";
        $mech->click_button( 'value' => 'Send Spam Report(s) Now' ) ."\n";
    } else {
        print "No report form button found.\n";
        $stop = 1;
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .