Shetty's
Shetty's

Reputation: 617

How to read value from Password Field in Selenium/Webdriver

I have scenario where in i want to read values from excel and put into username and password field, later want to verify whether username and password got entered in the are proper by comparing with Excel values.

Below is sample code given w.r.t to "gmail". HTML Code:

<input type="password" name="Passwd" id="Passwd">

Webdriver Code:

Driver.driver.get("https://www.gmail.com");
Driver.driver.findElement(By.name("Passwd")).sendKeys("mahesh");
    System.out.println(Driver.pFLogin.getTxtPassword().getAttribute("value"));

Getting error for above code, since password field dont have "value" attribute

Kindly help me on this..

Thanks Mahesh

Upvotes: 2

Views: 10177

Answers (3)

Krzysztof Walczewski
Krzysztof Walczewski

Reputation: 670

If You want to see the password (eg. on screen or while executing test) you can use JSexecutor.

        JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("var arr = document.getElementsByTagName(\"input\");\n" +
            "for (var i = 0; i < arr.length; i++) {\n" +
            "    if (arr[i].type == 'password') arr[i].setAttribute('type','text');\n" +
            "}");

    String password = fPassword.getAttribute("value");
    logger.debug("pass:" +password);

Upvotes: 0

Ravi Potnuru
Ravi Potnuru

Reputation: 191

try this code once

File sr=new File("File path");
    //load file
    FileInputStream fis=new FileInputStream(sr);
    //load workbook
    XSSFWorkbook wb=new XSSFWorkbook(fis);
    XSSFSheet wsh=wb.getSheetAt(0);
    //read data
    String s1=wsh.getRow(0).getCell(0).getStringCellValue();
    String s2=wsh.getRow(0).getCell(1).getStringCellValue();
    String s3=wsh.getRow(0).getCell(2).getStringCellValue();
    System.out.println(s1+" "+s2+" "+s3);
    Thread.sleep(3000); 
    fis.close();
    WebDriver d1=new FirefoxDriver();
    d1.get("website url");
    d1.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    Thread.sleep(10000);
    d1.findElement(By.xpath("//*[@id='username']")).sendKeys(s2);
    d1.findElement(By.xpath("//*[@id='password']")).sendKeys(s3);
    d1.findElement(By.xpath("xpath of the element")).click();
    Thread.sleep(3000);
    d1.close();


    FileInputStream fs=new FileInputStream("file path");
    Workbook wb1=WorkbookFactory.create(fs);
    Sheet sh=wb1.getSheetAt(0);
    Row row=sh.createRow(row value);
    //Write data
    Cell cell=row.createCell(cell value);
    cell.setCellType(Cell.CELL_TYPE_STRING);
    cell.setCellValue("ravi");
    FileOutputStream fos=new FileOutputStream("file path");
    wb1.write(fos);
    fos.close();

}

}

Upvotes: 0

Chetan
Chetan

Reputation: 2400

Try this,

System.out.println(Driver.findElement(By.id("Passwd")).getAttribute("value"));

make sure, By typing into an input element, you are changing its value attribute as text.

Upvotes: 2

Related Questions