Click on any html element using different locators

/**
 * Click on any html element in the webpage as long as you can use the locators to identify them.
 * Assume that you have Internet Explorer and Selenium Server is running on your
 *  computer on default port 4444. Otherwise, change accordingly in the constructor.
 * @Author: Xuan Ngo
 */
import com.thoughtworks.selenium.DefaultSelenium;
 
public class ClickAny
{
  private DefaultSelenium m_oBrowser = null;
 
  public static void main(String[] args)
  {
    ClickAny oClickAny = new ClickAny();
 
    // Click on any html element in the webpage.
    oClickAny.runExamples();
  }
 
  // Constructor
  public ClickAny()
  {
    this.m_oBrowser = new DefaultSelenium("localhost", 4444, "*iexplore", "http://openwritings.net/");
    this.m_oBrowser.start(); // Start Selenium.
    this.m_oBrowser.open("http://openwritings.net/sites/default/files/ClickAnyElementExample.html"); // Open the webpage.
  }
 
  // Click on any html element in the webpage.
  public void runExamples()
  {
    // Click on Submit button using NAME locator.
    this.m_oBrowser.click("name=submit_button"); 
 
    // Click on <li> using ID locator.
    this.m_oBrowser.click("id=unique-link-id");
 
    // Click on <li> using XPATH locator.
    this.m_oBrowser.click("xpath=//li[contains(text(), 'using the xpath')]"); 
 
    //this.m_oBrowser.stop(); // Close Selenium.
  }
}
AttachmentSize
HTML icon ClickAnyElementExample.html1.24 KB