Selenium WebDriver : Extracting All Text Fields From Web Page

Sometimes you need to extract specific types of web elements from software web page like extract all Links to open all of them one by one, extract all text boxes from page to type some text In all of them or In some of them one by one. Previously we have learnt how to extract all links from software web page In THIS POST. Now supposing you have a scenario where you needs to extract all text fields from software web page to send some text In all of them how to do It In selenium WebDriver?

WebDriver's In built findelements method will help us to find all text boxes from software web page. If you know, Each simple text box are Input fields and will have always attribute type = text and If It Is password text box then It's type will be password as shown In bellow given Image.

In short we have to find all those Input fields where type = "text" or "password". In bellow given example, I have used findelements method to find and store all those elements In txtfields array list and then used for loop to type some text In all of them.

package Testng_Pack;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Extract_Text {
 WebDriver driver;
 public void StartBrowser() {
  System.setProperty("webdriver.gecko.driver", "D:\\Selenium Files\\geckodriver.exe");
  driver = new FirefoxDriver();
 public void Text() throws InterruptedException{
  //find all input fields where type = text or password and store them In array list txtfields.
  List<WebElement> txtfields = driver.findElements(By.xpath("//input[@type='text' or @type='password']"));
  //for loop to send text In all text box one by one.
  for(int a=0; a<txtfields.size();a++){   
 public void CloseBrowser() {  

This way we can extract all text box from software web page If required In any scenario.

1 comment:

  1. txtfields.get(a).sendKeys("Text"+(a+1)); this part of the code is not working sir, its gives
    FAILED: Text
    org.openqa.selenium.InvalidArgumentException: Expected [object Undefined] undefined to be a string
    Build info: version: 'unknown', revision: '5234b32', time: '2017-03-10 09:00:17 -0800'
    System info: host: 'DESKTOP-U74AH3C', ip: '', 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'
    Driver info: org.openqa.selenium.firefox.FirefoxDriver