Sunday, September 19, 2021

findElement and findElements


findElement and findElements are selenium commands which are used to locate elements in Web pages using different ways such as


ID driver.findElement(By.Id("Login"));
Name driver.findElement(By.Name("Login"));
ClassName driver.findElement(By.ClassName("Login"));  
LinkText driver.findElement(By.LinkText("Login"));
PartialLinkText driver.findElement(By.PartialLinkText("Login"));
TagName driver.findElement(By.TagName("Login"));
XPath driver.findElement(By.XPath("Login"));

findElement() 

the command used to identify a web element in the web page.

  • This command is used to fetch a single element on the web page
  • It will return the object of the first matching element
  • It will throw NoSuchElementException when there is no element

driver.findElement(By.xpath("//*[@Value='Yes']")).click();

Ex:





findElements() 

the command used to identify a list of web elements in the web page.

  • This command is used to uniquely identify the list of web elements within the web page.
  • If the element doesn’t exist on the page then if no element is found it will return the empty list.

WebElement element = driver.findElement(By.LocatorStrategy("Locator"));

Ex: 












No comments:

Post a Comment