ToxicGlow
ToxicGlow

Reputation: 71

How to interaction with a webpage in Java?

Now, I am a beginner in Java. I have somehow managed to understand the following code.

 import java.net.*;
  import java.io.*;

 public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL yahoo = new URL("http://www.yahoo.com/");
        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

I actually got that with quite some effort so please do understand I am a beginner. I want to interact with the webpage. From the code, I understood whatever information of the webpage will be simply displayed. I just need your help to advice me wha should I study next.

I want my webpage to go to the webpage, login Then click on a button Do a comparision between two columns and report it to me if it isn't equal.

I did do reading on HTTP and I know webinteraction is possible. People have code out there that is above my understanding.

I don't know much about inheritence or encapsulation [still learning](incase it is required)

Using the code I provided, is it possible to add on my requirements? Cause people are giving one lined codes or 30 lined code...please realize I am not so good in it.

I did do research .I just want someone to give me direction. I am starting to get confused due to so many methods.

I think someone told me php is easier on this matter, but in php I don't even know anything. (I know it is OOPS language but still)

Any sort of guidance is truely appreciated.

Upvotes: 3

Views: 6427

Answers (1)

Jaffar Ramay
Jaffar Ramay

Reputation: 1165

If you actually need to interact with the website then selenium/webdriver is perfect for your needs:

http://code.google.com/p/selenium/wiki/GettingStarted

Sample Google search:

package org.openqa.selenium.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class Example  {
    public static void main(String[] args) {
        // Create a new instance of the html unit driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = new HtmlUnitDriver();

        // And now use this to visit Google
        driver.get("http://www.google.com");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));

        // Enter something to search for
        element.sendKeys("Cheese!");

        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
    }
}

Upvotes: 4

Related Questions