How to program bots in Java?
So, what about writing bots in Java? Question only looks hard but have very simple answer:
Use:
Using HtmlUnit to writing bots pretty good described.
What is Selenium?
Selenium is testing framework that consist of Firefox plugin that can record user’s actions in browser and Java (several another languages as well) API allowed to “play” this record from Java application.
While HtmlUnit has some limitations, Selenium (or bot written using Selenium) can do all what user can do in Firefox.
Bots programmed with HtmlUnit can be multithreaded (very fast) but some times have problem with Javascript rich sites.
By the way Selenium can use HtmlUnit as “player” as well as Firefox and Chrome. Interesting question If HtmlUnit limitations applied to Selenium tests run on HtmlUnit.
Below is a code snippet from my program that submits link to one of the famous social bookmarking sites. Snippet shows what is the “user’s actions record” that selenium provides.
selenium.open("/");
selenium.type("password", password);
selenium.click("rememberme");
selenium.click("login-submit");
selenium.waitForPageToLoad("30000");
selenium.click("link=Favorites");
selenium.waitForPageToLoad("30000");
selenium.click("link=Add a site");
selenium.type("url", url);
selenium.type("review", review);
selenium.click("link=Add a site >");
selenium.type("topic", topic);
selenium.click("sfw");
selenium.click("//input[@value='Submit >']");
selenium.waitForPageToLoad("30000");