How to make Java applications interact with websites

I have a program to get data from Excel files and operate it for users However, in order to get the update of Excel file, you need to download it from the website I initially tried to navigate to the website using a robot human, log in with a user name and password, then navigate to the correct part of the website, find the "download Excel spreadsheet" button and click it But I know it's a terrible way. It doesn't always work

Solution

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

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

Google search example:

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());
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>