Type characters

For similar example using Selenium v1, see Type characters.

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
 
/**
 * Selenium v2: Type characters.
 * Assume that you have Firefox installed.
 * You need to change the encoding of your editor to support the Chinese characters below.
 * @Author: Xuan Ngo
 */
public class WritingExample2
{
  public static void main(String[] args)
  {
    // All actions will be applied to Firefox.
    WebDriver oWebDriver = new FirefoxDriver();
 
    // Open the webpage.
    oWebDriver.get("http://openwritings.net/sites/default/files/WritingExample.html");
 
    WebElement oInputElement = null;
 
    // Type characters using different locators and field types.
    oInputElement = oWebDriver.findElement(By.xpath("//*[@name='first-name']"));  // XPATH locator.
    oInputElement.sendKeys("Xuan");
 
    oInputElement = oWebDriver.findElement(By.cssSelector("input[name='last-name']"));  // CSS locator.(2010-05-30: IE driver doesn't support CSS locator.)
    oInputElement.sendKeys("Ngo");
 
    oInputElement = oWebDriver.findElement(By.className("red"));  // Class Name locator: the value of the "class" attribute.
    oInputElement.sendKeys("my password");
 
    oInputElement = oWebDriver.findElement(By.id("unique-id"));   // ID locator. Type Chinese characters. You need to change the encoding of your editor.
    oInputElement.sendKeys("中文");
 
    oInputElement = oWebDriver.findElement(By.name("key-press")); // Name locator.
    oInputElement.sendKeys("auto complete");
 
    pause(10000);
 
    // Close the browser.
    oWebDriver.close(); 
  }
 
  /**
   * Pause for X milliseconds.
   * @param iTimeInMillis
   */
  public static void pause(final int iTimeInMillis)
  {
    try
    {
      Thread.sleep(iTimeInMillis);
    }
    catch(InterruptedException ex)
    {
      System.out.println(ex.getMessage());
    }
  }
 
}
AttachmentSize
HTML icon WritingExample.html1.53 KB