简体   繁体   中英

Java and Selenium: Trouble getting contents of input field

I'm having problems getting the text contents of an input field. I seem to only be getting the things around it with the method I'm using.

Snippet from the page:

(It's a list of itemsincluding an input field in each row.)

在此处输入图像描述

The markup:

<ul class="budsjett budsjett--kompakt" id="sifobudsjett">
   <li class="budsjett-post ng-isolate-scope ng-valid" id="SIFO_mat">
      <div class="felt" >
         <div class="felt-indre">
            <div id="SIFO_mat-farge" class="sifo-farge farge-graa"></div>
               <span class="budsjett-post-beskrivelse" >
                  <span tabindex="0" title="Vis hjelpetekst" role="button">
                     <span class="hjelpetekst-label" >Mat og drikke</span>
                  </span>
                  <span class="sifo-hjelp" aria-hidden="true"></span>
               </span>
            </span>
            <span class="budsjett-post-verdi">               
               <span class="budsjett-post-verdi-endret" ng-show="!skrivebeskyttet" aria-hidden="false" style="">
                  <input id="SIFO_mat-input" name="SIFO_mat" type="number">
                  <span class="felt-enhet"><abbr id="SIFO_mat-enhet" title="kroner" translate=""><span class="ng-scope">kr</span></abbr></span>
                  </span>
               </span>
        </div>
    </div>
</li>

The code:

List<WebElement> sifoliste = driver.findElement(By.id("sifobudsjett")).findElements(By.tagName("li"));

Result of first element: "Mat og drikke".

List<WebElement> sifoliste = driver.findElement(By.id("sifobudsjett")).findElements(By.tagName("input"));

Result of first element: ""

List<WebElement> sifoliste = driver.findElement(By.id("sifobudsjett")).findElements(By.className("budsjett-post-verdi-endret"));

Result of first element: "kr"

Any ideas?

The <input> tag doesn't have text, what you see in the UI is kept in the value attribute. It exists even if you can't see it in the html

driver.findElement(By.id("SIFO_mat-input")).getAttribute("value");

For all the <input> s

List<WebElement> sifoliste = driver.findElement(By.id("sifobudsjett")).findElements(By.tagName("input"));
String text = sifoliste.get(0).getAttribute("value"); // 2790

Try

String inputValue = driver.findElement(By.tagName("input")).getAttribute("value");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM