Showing posts with label click() webdriver function. Show all posts
Showing posts with label click() webdriver function. Show all posts

Wednesday, August 13, 2014

Example selenium webdriver code to click button/link/any kind of element using java


Example 1: 

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver(); // launch the firefox browser
driver.get("http://www.google.com"); // Navigate to google.com
driver.findElement(By.name("q")).sendKeys("test"); // send 'test' value in google search textbox
driver.findElement(By.name("btnG")).click(); // click on google search button

}

}

Example 2: Using mouse action class in selenium

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class Test {

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver(); // launch the firefox browser
driver.get("http://www.google.com"); // Navigate to google.com
driver.findElement(By.name("q")).sendKeys("test"); // send 'test' value in google search textbox
Actions action = new Actions(driver);
  action.click(driver.findElement(By.name("btnG"))).build().perform(); // click on google search button

}

}

Example 3: Using JavascriptExecutor class in selenium

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.JavascriptExecutor;

public class Test {

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver(); // launch the firefox browser
driver.get("http://www.google.com"); // Navigate to google.com
driver.findElement(By.name("q")).sendKeys("test"); // send 'test' value in google search textbox
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.name("btnG"))); // click on google search button

}

}