简体   繁体   中英

Webdriver getText method with truncate using Java

Html Code

<table id="tblRenewalsFiled" class="StatusList" width="100%" cellspacing="0" cellpadding="0" border="0">
  <tbody data-bind="foreach: RenewalFilterModels">
   <tr>
    <td>
      <a id="aFilterMenu_NotAudited" class="aFilter" 
         data-bind="click: $parent.ShowFilter, attr:{'id':'aFilterMenu_' + StatusName}" href="#">
Not Audited
        <span class="count">13</span>
      </a>
    </td>
  </tr>
  </tbody>
</table>

from the above, when I use the getText , method it will return " Not Audited 13 "

String filterValue = driver.findElement(By.id("aFilterMenu_NotAudited").getText();

my expectation was only for Not Audited , provide an solution or suggestion to truncate the span class count ie "13"

  1. So all I can to advice is to take cssSelector "a.aFilter, span", it will return an array of two elements [ 'Not Audited 13', '13' ]

    String[] value = driver.findElement(By.id("aFilterMenu_NotAudited").getText();

then you are truncating the second element from the end of the first one:

String myText = value[0].substring(0, value[0].length() - value[1].length());

Looks like a bit tricky.

  1. Another way is using xpath:

//a[@class='aFilter'][not(self::span)]/text()

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