Skip to main content

Making Android web bots that click on links - x-droov

As usual your bot will not be able to properly emulate human behavior if it can't do a simple click. In this part of our Android web bot guide, we are going to go through how to make a bot that clicks on an offline page.
After making that bot, we'll also make one that fills out forms in html and submits them.
That's a great bot isn't it? 

Android web bot that clicks buttons

If it is, lets start off. We have a simple html file with a button which when clicked will turn our heading red. It's code is here

<html>

    <body>

        <h1 id="em">This is our heading</h1>

 

  <script>

     function tg(){

document.getElementById("em").style.color="red";

}

  </script>      

<button href="#em" id="lnk" onclick="tg()">change

   </button>

   </body>

</html>


That's a simple example of a real web page. You'll first study the web page's code to know what to call. Like in the above web page, we just need to call the tg() function in java and our bot will do the needful.
Here's the code that calls the tg() function in code without you tapping on any html element

package com.weby;

import java.util.Base64;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.webkit.WebView;

import android.webkit.WebViewClient;

public class MainActivity extends Activity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.layout_main);

   WebView wv= (WebView) findViewById(R.id.weby); wv.getSettings().setJavaScriptEnabled(true);

        wv.setWebViewClient(new WebViewClient()); wv.loadUrl("file:///android_asset/dd.html");

  Button bb=(Button) findViewById(R.id.but);

        bb.setOnClickListener(new View.OnClickListener(){

          public void onClick(View v){

                wv.post(new Runnable() { 

                    @Override public void run() {

                  // Note that the JS 

                //method of javascript 

                     wv.loadUrl("javascript:tg()");  

                    } 

        });

            } 

        });

    }

}


That's it. You place your html file in the assets folder of your project then refer to it using:
wv.loadUrl("file:///android_asset/dd.html");
After you have loaded the page, you now need to perform the JavaScript scripting.
The method loadUrl() runs asynchronously so we call upon it in a new thread.
If you don't call it using a new thread, nothing will happen.

Android web bot that fills out form fields

Next on our list is a bot that fills out a form field. We are going to add a form field to our html and change some parts of our code.
The html is:

<html>

    <body>

        <h1 id="em">This is our heading</h1>

<form>

        <input id="in" type="text" />

    </form>

  <script>

     function tg(){

document.getElementById("in").value="red";

}

  </script>      

<button href="#em" id="lnk" onclick="tg()">change

   </button>

   </body>

</html>


Then our java code remains the same. Look at that example well, the code for filling up the form is in the html document. That's not the normal way websites are made. So, we're going to load the required JavaScript through java to enable us use values not provided in the html.
We modify the html below:

<html>

    <body>

        <h1 id="em">This is our heading</h1>

<form>

    Name:<br />

        <input id="in" type="text" />

    <br />

    Area:<br />

           <input id="in2" type="text" />

        </form>     

   </body>

</html>


Then our java code will be a better version with greater flexibility.

package com.weby;

import java.util.Base64;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.webkit.WebView;

import android.webkit.WebViewClient;

public class MainActivity extends Activity {

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);     setContentView(R.layout.layout_main);

   WebView wv= (WebView) findViewById(R.id.weby); wv.getSettings().setJavaScriptEnabled(true);

        wv.setWebViewClient(new WebViewClient()); wv.loadUrl("file:///android_asset/dd.html");

  Button bb=(Button) findViewById(R.id.but);

        bb.setOnClickListener(new View.OnClickListener(){

          public void onClick(View v){

                wv.post(new Runnable() {      

                    @Override public void run() {

                     String name ="Emmy Jayson";

                     String area="Uganda";   

                 wv.loadUrl("javascript:var p=document.getElementById('in').value='"+name+"';");

                  wv.loadUrl("javascript:var p=document.getElementById('in2').value='"+area+"';");     

                    } 

        });

            } 

        }); 

    }

}


When passing in our javascript code, we use variables like:
wv.loadUrl("javascript:var p=........" );
If you don't, the code will jam.
After you have learned how to fill out forms, it's now time to submit. This is what you do.
Let's assume the submit button has an id="submit_input"
We would use the javascript click method to perform a click just like the example below
wv.loadUrl("javascript: var p= document.getElementById('submit_input').click()");
That should work pretty well for you. No hustles.
This is for this part of the guide. You should remember to learn about the call javascript method which will enable you call listeners automatically.
That'll enable you perform longclicks, scrolls and many others without doing the actual thing.
Thanks for reading, the next article is on how to make your robot standard. These are insights into making real bots that give headache.
Making web bots that give headache
A complete insight-filled article to open you into how much havoc or happiness you can create using web bots. Let's meet there. 
Let me know about any issues in the comments. Bye.

AUTHOR

Emmy Jayson

Emmy Jayson



Comments