Sujith Kp
Sujith Kp

Reputation: 1105

How can I load a webpage in a java console application and click a button in that webpage programatically?

I want to load a webpage in a Java console application, fill some text fields, and submit it by clicking a submit button. Is there any Java library available for doing this?

Earlier I tried loading an IE object using powershell from a Java program. It worked well in some cases but I was getting some problems while loading webpages with multiple iframes, so I had to drop that approach and try thinking of a solution fully in Java.

Upvotes: 1

Views: 199

Answers (2)

Boris the Spider
Boris the Spider

Reputation: 61148

Just use Selenium, it's the usual way of automating browser interaction.

You simply create a WebDriver:

WebDriver driver = new InternetExplorerDriver();

And navigate to a page:

driver.get("http://google.com");

You can the select elements by id:

WebElement element = driver.findElement(By.id("coolestWidgetEvah"));

By class:

List<WebElement> cheeses = driver.findElements(By.className("cheese"));

Or even by XPath if the above two approaches don't suit you:

List<WebElement> inputs = driver.findElements(By.xpath("//input"));

There are many other ways to find elements on the page and to interact with the page. These examples are all taken from the Selenium Documentation which I suggest you read.

Upvotes: 1

Matsemann
Matsemann

Reputation: 21784

You can use a testing library like http://htmlunit.sourceforge.net/ which can automate clicks etc. on web pages.

Or instead, you can just send the wanted HTTP request (that would have been sent in the browser) directly in Java.

Upvotes: 1

Related Questions