How to get a Java application to interact with a website

I have a program that takes data from an excel file and manipulates it for the user. But in order to receive updates in the excel file, they must be downloaded from the website. First, I tried to use the robot class to go to the site, log in with a username and password, then go to the correct section of the website and find the button that says "load Excel spreadsheet" and click on it. But I understand that this is a terrible way to do this, and it does not always work. What's better, I can do this so that my program can go to the site and go to the page I want, and then upload the data. I read about the "broken page", but I do not think that this will allow me to do this. I really want to interact with a webpage so as not to load its contents. Any help would be great. Thanks Peter

+6
java
source share
3 answers

If you really need to interact with a website, then 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()); } } 
+12
source share

You can use http requests to download the file if you know the URL. a quick google found this: http://download.oracle.com/javase/tutorial/networking/urls/readingWriting.html download the file and save to disk

0
source share

I did not understand that we are now using the excel file for download. I can offer you the following solutions:

I think this should help you ...

0
source share

All Articles