Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get hidden values in webdriver using javascript

There's a hidden input field in which I'm trying to insert a specific date value. The field originally produces a value, from which a user can select an appropriate value. The page's source code looks like this:

<div id="change_img">
  <img width="80" height="30" border="1" src="http://jntuh.ac.in/results/images/CaptchaSecurityImages.php?width=100&height=50&characters=5&code=ryyrh">
  <br>
  <input id="code" type="hidden" value="ryyrh" name="code">
</div>
like image 655
Sunil Kumar Avatar asked Jan 13 '23 03:01

Sunil Kumar


1 Answers

Use WebElement's getAttribute method. In your case it will be:

WebElement hiddenInput = driver.findElement(By.id("code"));
String value = hiddenInput.getAttribute("value");

If for any reason you need to do it with javascript (your question specifically asked for js) then this code should work:

String script = "return document.getElementById('code').getAttribute('value');";
String value = ((JavascriptExecutor) driver).executeScript(script).toString();
like image 101
JacekM Avatar answered Jan 19 '23 03:01

JacekM