代码:
package com.funcybernation.yndextesting;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
/**
* Hello world!
*
*/
public class App {
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());
}
}
输出=====
HtmlTextInput[<input autocomplete="off" class="lst tiah" value="" title="Пошук Google" maxlength="2048" name="q" size="57" style="color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top;padding-right:38px" type="text">] -> HtmlHtml[<html itemscope="itemscope" itemtype="http://schema.org/WebPage">]
HtmlTextInput[<input autocomplete="off" class="lst tiah" value="" title="Пошук Google" maxlength="2048" name="q" size="57" style="color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top;padding-right:38px" type="text">] -> HtmlHtml[<html itemscope="itemscope" itemtype="http://schema.org/WebPage">]
HtmlTextInput[<input autocomplete="off" class="lst tiah" value="" title="Пошук Google" maxlength="2048" name="q" size="57" style="color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top;padding-right:38px" type="text">] -> HtmlHtml[<html itemscope="itemscope" itemtype="http://schema.org/WebPage">]
HtmlBody[<body bgcolor="#fff">] -> HtmlHtml[<html itemscope="itemscope" itemtype="http://schema.org/WebPage">]
HtmlTextInput[<input autocomplete="off" class="lst tiah" value="" title="Пошук Google" maxlength="2048" name="q" size="57" style="color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top;padding-right:38px" type="text">] -> HtmlHtml[<html itemscope="itemscope" itemtype="http://schema.org/WebPage">]
HtmlTextInput[<input autocomplete="off" class="lst tiah" value="Cheese!" title="Пошук Google" maxlength="2048" name="q" size="57" style="color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top;padding-right:38px" type="text">] -> HtmlHtml[<html itemscope="itemscope" itemtype="http://schema.org/WebPage">]
Page title is: Cheese! - Search Google