If you understand automation in java and JavaScript then getting on with creating an Android web bot becomes an easy task. I looked for guides on creating web bots, but I got different answers which never fulfilled what I wanted.
After that, I decided to take another direction into my research, automation. That's what got me to the real answer.
In this tri-article series, we are going to create a simple web bot using automation features.
What's a web bot
To my understanding, a web bot is an intelligent automation piece of code. Take an example, you needed to search for a certain word and retrieve certain results from all search engines.
You could make a code that automatically iterates through those search engines' URLs and retrieves what you want. At the end of it, the bot gives you what you want.
Requirements for you to make intelligent bots
The web is mastered on three main languages; HTML, CSS, and JavaScript. You must be very proficient with those languages to make web bots in any other language.
This is because, to make a web bot, you need to first understand the structure of a website then code your bot to navigate to parts you want it to.
In opposition, you could also make a bot that learns from what you do. That's a bot which clicks links. You could navigate to a site and click a link then your bot will automatically read the click and determine which element you clicked. Later, it'll be able to click that same link for you.
How web bots are made in java- Android
In Android, web bots work with the webview class. Through that class you can load XSS to control what is displayed at what moment. That means you could decide to do very rapid web actions within a few clicks.
To make our bot, we have to first make the lowest of bots, the browser bot.
This bot will load a page automatically then display it for us just like a normal browser.
If you don't understand html and JavaScript well, first go and learn them well or research what you don't know. I covered an introduction to web design so, you could first start there.
The browser bot
1. Layout
In your Android layout, add a button and a webview. The button fires our bot.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click me"
/>
<WebView
android:id="@+id/weby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Then in our main activity, we call upon the webview to load our page.
2. Java
WebView wv= (WebView) findViewById(R.id.weby); wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new WebViewClient());
Button bb=(Button) findViewById(R.id.but);
bb.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
wv.loadUrl("https://www.google.com");
}
});
The setJavaScriptEnabled() method allows your webview to load JavaScript based webpages.
setWebViewClient() allows your webview to display web content and not refer to your existent browser like chrome. This makes your app a complete browser. If you wondered how guys made Android html editors, just know that they simply used the webview class.
That's a web bot. What if we want our robot to load several pages one after the other.
You would use the JavaScript onload listener with a setTimeout() timing function that breaks within the first iteration. The delay will be the time you want a page to be visible before the next is loaded.
You could even use a new thread to load the next page async so that there's no black out on your screen.
This is what we are created but it's an introduction to what we are going for.
This is the first article. Check out the next article.
Making an Android web bot that clicks on links
In this article, we are covering all types of clicks from links to buttons and of course firing some listeners in a web page.
Be sure to check on it.
Keep the love spreading, share the article with your friends, comments any issues and never forget to ask any questions.
AUTHOR
Emmy Jayson
Comments
Post a Comment
Put your comment here