Selenium Find Element By XPath

Profile picture for user arilio666

The user needs to find the location of the web element to interact with it. Xpath can be written for this element. Now that we know the syntax for xpath let us discuss how we can find elements by XPath in selenium.

driver.findElement(By.xpath("Your Xpath"));
  • Selenium provides two webelement commands such as findElement and findElements.
  • FindElement gets a single web element xpath.
driver.findElement(By.id("value"));
  • FindElements gets a list of web element xpath.
List<WebElement> elementName = driver.findElements(By.xpath("value"));
  • Let us see an example code of finding an element by xpath.
WebDriverManager.chromedriver().setup();
        ChromeDriver driver = new ChromeDriver();
        driver.get("http://www.programsbuzz.com/user/login");
        driver.manage().window().maximize();
        driver.findElement(By.xpath("//input[@id='edit-name']")).sendKeys("Ash");
        driver.findElement(By.xpath("//input[@id='edit-pass']")).sendKeys("Pass");
  • A simple login page username and password filling automation.
  • Here we have used find element by xpath to fill in the username and password using the id xpath selector.