Selenium click element using click() method

Click element in selenium using click() method

Selenium click element can be done using click() method. Click is most common action to work on any web page. You need to click on buttons, drop down list, textbox, link, radio button, drag and drop or any other element. Click() method will help you to perform click action on any web element.

selenium click method

To perform click action using click() method in selenium, You first need to locate that specific element and then you can perform click action on it.

  • click() method syntax : driver.findElement(By.element_locator).click();
  • click() method is part of WebElement interface which extends to SearchContext and TakesScreenshot interfaces.
  • click() method emulates mouse single click action.
  • You can not perform double click action using click() method in selenium.
  • You can click on any of the web element using click() method in selenium.

Selenium click() method with example

Let us see how to use click() method in selenium to perform click action with practical example.

package testPackage;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ClickExample {

	public static void main(String[] args) {
		
		System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
		WebDriver driver=new ChromeDriver();
	    		
	    driver.get("http://only-testing-blog.blogspot.com/2013/09/testing.html");
		
		//Find test link element by xpath and click on it.
		driver.findElement(By.xpath("//span[normalize-space()='Test Link']")).click();
		
		//Find login button element by xpath and click on it.
		driver.findElement(By.xpath("//input[@value='Login']")).click();
		
		//swith to alert and perform OK button click action.s
		driver.switchTo().alert().accept();		
	}
}

In above given example, First click command will click on link with link text Test Link. It will redirect to new page. Second click method will click on Login button. It will generate alert which will be accepted in next sentance.

No comments:

Post a Comment