简体   繁体   中英

How to get the text only from the child element - Webdriver - Java

I am trying to get the text only from a child element. See below:

<strong class="EnvMain">
  <strong id="currentClock">11:19</strong>
  GMT
</strong>

I would like to get only the GMT text.

I tried writing the xpath like: .//*[@id='userEnvironmentInfo']/div[2]/a/strong/text()] but this way the element is not found.

Thanks in advance.

Update of HTML:

<div class="DateTime">
 <a class="EnvPicker" title="Change your timezone" href="javascript:void(0);">
  <span class="EnvDD">▾</span>
  <span class="EnvIcon DateTimeIcon">The time is:</span>
  <strong class="EnvMain">
    <strong id="currentClock">17:34</strong>
    GMT
    </strong>
  <span id="currentDay" class="EnvMore">Monday</span>
  <span id="currentDate" class="EnvMore">14.04.2014</span>
 </a>
 <div class="EnvContainer">
   <ol id="timeZoneOptions" class="EnvList">
      <li class="EnvItem">
         <a class="EnvOption" title="Set the timezone to GMT-12" onclick="return false;" rel="-12" href="javascript:void(0);">
             <strong class="EnvMain">GMT-12</strong>
             <span class="EnvMore">Current time:01:25</span>
         </a>
      </li>
      <li class="EnvItem">
         <a class="EnvOption" title="Set the timezone to GMT-11" onclick="return false;" rel="-11" href="javascript:void(0);">

and here the elements will continue until GMT +12.

The xpath you're searching for is:

//strong[@class='EnvMain']/text()

This xpath returns text, not a web element.

If you want to get text using selenium + java you can try the following:

driver.findElement(By.xpath("//strong[@class='EnvMain']")).getText();

Seems like getText Function will not return only GMT . But we can parse a string like this after getting the text:

    String s = driver.findElement(By.xpath("//strong[@class='EnvMain']/strong[id='currentClock']/..")).getText();
    s = s.substring(s.lastIndexOf(' ') + 1);

Use the following xpath to find the element:

//strong[@class='EnvMain']/strong[@id='currentClock']/..

What this xpath does is it finds the <strong> element with class EnvMain that has a child <strong> with an id of currentClock. (The .. at the end walks back up the dom to the parent element).

Then extract the text with the getText() method:

   String gmt = driver
        .getElement(By.xpath("//strong[@class='EnvMain']/strong[id='currentClock']/.."))
        .getText();

Then, if you want to ignore the text in the inner <strong> element and only get the timezone ("GMT")... there's not a good way to do this with xpath. You'll have to use a regular expression in Java to remove the part you don't want:

gmt = gmt.replaceAll("[\\d][\\d]?:[\\d][\\d]\\s*", "");

getText() returns null in your case, because in list item there is anchor tag and then text for anchor tag.So use getAttribute("innerHTML"). But you will not be able to select the item in list.

WebElement e1 = driver.findElement(By.xpath("//ul[@class='EnvContainer']"));

List<WebElement> list = e1.findElements(By.tagName("li"));
for(WebElement item: list)
 {
    String s = item.getAttribute("innerHTML");
    System.out.println(item.getAttribute("innerHTML"));
    }

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