1

I am trying to retrieve a value from an input field with Selenium in Java. Here is the relevant HTML code:

<input class="form-control input-sm " id="NameInputID" type="text" maxlength="16" name="NameInput" value="TheValueWanted" readonly="">

I have tried:

driver.findElement(By.id("NameInputID")).getText();

and

driver.findElement(By.id("NameInputID")).getAttribute("value");

None of them returned with the value(blank text). How should i deal with this?

Thanks!

Andrei Suvorkov
  • 5,559
  • 5
  • 22
  • 48

2 Answers2

0

You can try via JavascriptExecutor :

Thread.sleep(3) // add a pause to wait until value of the element will be rendered
JavascriptExecutor je = (JavascriptExecutor) driver;
String script = "return document.getElementById('NameInputID').getAttribute('value');");
String value = je.executeScript(script);
Andrei Suvorkov
  • 5,559
  • 5
  • 22
  • 48
  • So I guess there is an additional ( in the script line, because it ends with one without a pair. Anyway now it came back with an exception saying that org.openqa.selenium.WebDriverException: unknown error: document.getElementsById is not a function –  Jul 17 '18 at 13:38
  • I have in the code `getElement` not `getElements`, please try my code one more time – Andrei Suvorkov Jul 17 '18 at 13:41
  • It came back with an empty line. –  Jul 17 '18 at 13:49
  • Then try to add a pause before you execute a script. Make sure, that element has value you want to get. – Andrei Suvorkov Jul 17 '18 at 13:50
  • 1
    This did the job. Thanks! –  Jul 17 '18 at 13:59
  • @MilanSz. happy to hear it! So the pause helped? Like in my edited code? I'm asking for the future readers – Andrei Suvorkov Jul 17 '18 at 14:05
  • It seems that the pause helped, although I am uncertain why. Probably because it is only step among several and the script wanted to execute on a not yet loaded form. –  Jul 17 '18 at 14:54
  • Could you please also try `WebDriverWait` instead of `Thread.sleep(3)`? I have added in the answer – Andrei Suvorkov Jul 17 '18 at 15:05
  • I tried it, but it did not work. The returned value was blank, empty. I used the piece of code you added. –  Jul 19 '18 at 10:54
  • Ok thank you for feedback, I'll rollback my answer to the working one. It is for the future readers – Andrei Suvorkov Jul 19 '18 at 10:55
  • 1
    It is interesting, because at first thought, webdriverWait was a good idea. Perhaps it isn't about the element being visible, but the value not being present just yet. Anyway, thanks for the comments! –  Jul 19 '18 at 10:57
0

As per the HTML you have shared to extract the value from an input field i.e. TheValueWanted you need to be more specific with your Locator Strategy and you need to induce WebDriverWait for the element to be visisble and you can use either of the following solutions:

  • cssSelector:

    String myText = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.form-control.input-sm#NameInputID"))).getAttribute("value");
    
  • xpath:

    String myText = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@class='form-control input-sm' and @id='NameInputID']"))).getAttribute("value");
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • It came back with an empty line, unfortunately. –  Jul 17 '18 at 13:44
  • @MilanSz. Checkout my updated answer and let me know the status – undetected Selenium Jul 17 '18 at 13:48
  • It came back with the following exception, with both: `org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //input[@class='form-control input-sm' and @id='NameInputID'] (tried for 20 second(s) with 500 milliseconds interval)` –  Jul 17 '18 at 13:55