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;

public class ClickExample {

	public static void main(String[] args) {
		System.setProperty("", "D:\\chromedriver.exe");
		WebDriver driver=new ChromeDriver();
		//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.
		//swith to alert and perform OK button click action.s

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