How to interaction with a webpage in Java?

时光总嘲笑我的痴心妄想 提交于 2019-12-24 04:17:09

问题


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.


回答1:


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());
    }
}


来源:https://stackoverflow.com/questions/21286746/how-to-interaction-with-a-webpage-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!