package org.openqa.selenium.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Selenium2Example {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver();
// 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());
//Close the browser
driver.quit();
}
}
according to the website, http://seleniumhq.org/docs/03_webdriver.html , at first, i cannot make it run, because i didn't get the firefox of right version which's supposed to be english version 4 . perhaps english version is mandated. and the necessary jars respectively are selenium-java-2.0b3.jar, guava-r08.jar, httpclient-4.0.2.jar, httpcore-4.0.1.jar, commons-logging-1.1.1.jar, json-20080701.jar, the version of the jar may not necessarily be those. all are contained in the lib.zip file.
RemoteWebDriver
You’ll start by using the HtmlUnit Driver. This is a pure Java driver that runs entirely in-memory. Because of this, you won’t see a new browser window open.
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 HtmlUnitExample {
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());
}
}
and all the jars are in the download page http://seleniumhq.org/download/ , selenium2.0b3.