How to type some text in hidden field in Selenium WebDriver using Java
Asked Answered
R

3

23

I am using WebDriver with Java for test automation. I have the following HTML code for input field which is hidden:

<input type="hidden" value="" name="body" id=":6b">

How to type something in hidden field in Selenium2 (WebDriver)? I have written code as:

driver.findElement(By.name("body")).sendKeys("test body");

But it was shown the following error: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 30.04 seconds

Can anybody please help me to write/type some text in hidden field?

Responsibility answered 8/8, 2012 at 5:49 Comment(1)
One thing to add: id is dynamic. Here id=":6b" , sometimes I found id=":3y"Responsibility
A
32

First of all you have to change the value of type attribute as text from hidden. The following code using javascript would work for that:

jse.executeScript("document.getElementsByName('body')[0].setAttribute('type', 'text');");

Now, you are able to type on that text by using WebDriver. So, the overall code for typing in a hidden field with WebDriver using Java and Javascript as follows:

WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementsByName('body')[0].setAttribute('type', 'text');");
driver.findElement(By.xpath("//input[@name='body']")).clear();
driver.findElement(By.xpath("//input[@name='body']")).sendKeys("Ripon: body text");
Acroterion answered 8/8, 2012 at 8:32 Comment(7)
I guess setting a value with JS should work as below: jse.executeScript("document.getElementsByName('body')[0].value='Ripon Al Wasim as body text';");Acroterion
When i use the code u have given, i am getting the NullPointerException error on the line jse.executeScript("document.getElementsByName('body')[0].setAttribute('type', 'text');");Mandolin
Actually my field is visible but i am not able to enter the data in that field.Mandolin
@Umamaheshwar Thota: Can you please provide the HTML code for this field?Acroterion
That question i have asked.Mandolin
Umamaheshwar Thota : As your field is visible (it means the field is not hidden) you can skip the line jse.executeScript("document.getElementsByName('body')[0].setAttribute('type', 'text');");Acroterion
js.executeScript("document.getElementsByName('ToDate')[0].setAttribute('type', 'text');"); <-- Worked for me, thank you!Hokusai
M
5
WebDriver driver=new FirefoxDriver();
driver.get("http://localhost/login.do");
driver.manage().window().maximize();
RemoteWebDriver r=(RemoteWebDriver) driver;
String s1="document.getElementById('username').value='admin'";
r.executeScript(s1);
Monosepalous answered 4/9, 2016 at 16:18 Comment(0)
L
0

you need to initialize a JavascriptExecutor, which will perform a javaScript command:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('ElementId').setAttribute('type','text');");
driver.findElement(By.id("ElementId")).click();
driver.findElement(By.id("ElementId")).clear();
driver.findElement(By.id("ElementId")).sendKeys("theTextYouWant");

and if you want to hide it :

js.executeScript("document.getElementById('ElementId').setAttribute('type','hidden');");
Larochelle answered 25/3, 2021 at 16:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.