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.
  • 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.
        ChromeDriver driver = new ChromeDriver();
  • 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.