I am new to XPath and trying to capture the values "Time: " and "13:45" from the following HTML snippet. Any help or suggestion will be really useful. Thank you!
<div class="inner-box">
<p class="inner-info-blk">
<strong>Time: </strong>
"13:45"
</p>
</div>
I can access the label with in the <strong>...</strong>
container with the pattern below but cannot figure out how to get the time value with in the <p ...>
container.
Label xpath:
//div[@class="inner-box"]/p[@class="inner-info-blk"]/strong
3
Answers
You can use
text()
to get the text from an element.And that would get the text from the
<p>
element.UPDATE:
As @shailesh has mentioned, the Selenium locator would not evaluate XPath expression that returns a text; nor, to the best of my knowledge, there exists such a method in Selenium that will evaluate arbitrary XPath expression. But just to offer an alternative, you may also use a bit of JS here:
You can find out the solution using split method, because Locators do not allow to use text() method with xpath. Time: in your example is a static and unique value which can split to get actual time value what you expect. I would recommend to first deal with xpath, if not found the solution try to resolve by logic. May be this can help you.
O/P:
"13:45"
This can be also relevant
O/P:
"13:45"
Given the HTML:
The time value i.e. 13:45 is a within a Text Node_ and the lastChild of it’s parent
<p>
. So to extract the desired text you can use either of the following locator strategies:Using xpath,
execute_script()
and textContent:Using xpath,
get_attribute()
andsplitlines()
:Alternative
As an alternative you can also use Beautiful Soup as follows:
Code Block:
Console Output:
References
You can find a couple of relevant detailed discussions in: