Thursday 13 June 2013

Selenium WebDriver : findElement() Vs findElements() functions



It is very important to know the difference between findElement() and findElements() functions and here you go for it :

findElement() :

  1. Find the first element within the current page using the given "locating mechanism".
  2. Returns a single WebElement.
  3. Syntax: WebElement findElement(By by)

Ex:
  1. driver.get("https://signup.live.com");  
  2. WebElement firstName=driver.findElement(By.id("iFirstName"));  
  3. firstName.sendKeys("Automated First Name");  


findElements() :

  1. Find all elements within the current page using the given "locating mechanism".
  2. Returns List of WebElements.
  3. Syntax:  java.util.List<WebElement> findElements(By by)

Ex:
  1. driver.get("https://signup.live.com");  
  2. List <webelement> textboxes1=driver.findElements(By.xpath("//input[@type='text']"));  
  3. System.out.println("total textboxes "+textboxes1.size());  
  4.   
  5. </webelement>  

No comments:

Post a Comment