Locate a button and click on it

In this section, we will see how to locate a button element and perform click operation on it. Lets take an example of Facebook page where we will click on 'Create Account' button using selenium webdriver.


Locate a button in Selenium webdriver and click on it

Inspect Element:
Locate a button in Selenium webdriver and click on it

Example Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo {

 public static void main(String[] args) {
  // TODO Auto-generated method stub

  System.setProperty("webdriver.gecko.driver", "Driver/geckodriver.exe");
  WebDriver driver = new FirefoxDriver();
  driver.get("https://www.facebook.com/");

  // Locate a button and click on it
  WebElement btn = driver.findElement(By.id("u_0_e"));
  btn.click();
 }
}


<-- Previous || Next -->

No comments:

Post a Comment