Reputation: 59
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.
Upvotes: 2
Views: 10663
Reputation: 3258
It's not possible to retrieve a text node using WebDriver. An XPath expression can refer to a text node, but the result of the expression can't return a text node, only an element node.
So you will have to do some string manipulation on the client side (i.e. in Java). You can retrieve the outer strong
element and the inner strong
element, and essentially subtract the inner one from the outer one to effectively reproduce the text node which is within the outer strong
but not the inner one.
<strong class="EnvMain">
<strong id="currentClock">17:34</strong>
GMT
</strong>
You mention that there are multiple strong[@class='EnvMain']
elements, but you can find the one you want by relying on the uniqueness of the @id
attribute of its child strong
element. e.g.
//strong[@class='EnvMain'][strong/@id='currentClock']
To get the inner strong
element:
//strong[@id='currentClock']
Then use Java String
methods, e.g.
String timezone = outerStrong.getText().substring(
innerStrong.getText().length()
);
Upvotes: 0
Reputation: 11
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"));
}
Upvotes: 0
Reputation: 8294
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*", "");
Upvotes: 0
Reputation: 1805
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);
Upvotes: 0