Selenium with Java 52 - How to get Hidden Field values using JavascriptExecutor interface

Published: 11 December 2018
on channel: Ankpro Training
6,487
46

What is JavaScript?
JavaScript is the preferred language inside the browser to interact with HTML dom.
This means that a Browser has JavaScript implementation in it and understands the JavaScript commands.

What is JavaScriptExecutor? 
JavaScriptExecutor is an Interface that helps to execute JavaScript through Selenium Webdriver.

JavaScriptExecutor provides two methods "executescript" & "executeAsyncScript“ to run JavaScript on the selected window or current page.

Hidden Field:
The input type="hidden" defines a hidden input field.
A hidden field let web developers include data that cannot be seen or modified by users when a form is submitted.
We can not get those values using only selenium but we get those values using JavaScriptExecutor along with selenium

How to get hidden field values using JavaScriptExecutor?
By using this code we will get hidden field values
((JavaScriptExecutor)driver).executeScript(" return document.getElementById('Id').value").ToString();

Possible Interview Questions on hidden field in selenium
How to get hidden field values using JavaScriptExecutor

Code :
@Test
public void HiddenField()
{
WebDriver driver = new FirefoxDriver();
driver.get("http://uitestpractice.com/Students/Index");
driver.findElement(By.xpath("//tbody//tr[2]//td[4]//button[1]")).click();
String s = ((JavascriptExecutor)driver).executeScript(" return document.getElementById('Id').value").toString();
System.out.println(s);
driver.quit();
}


On this page of the site you can watch the video online Selenium with Java 52 - How to get Hidden Field values using JavascriptExecutor interface with a duration of hours minute second in good quality, which was uploaded by the user Ankpro Training 11 December 2018, share the link with friends and acquaintances, this video has already been watched 6,487 times on youtube and it was liked by 46 viewers. Enjoy your viewing!