XPath to element text - xpath

<p><span class="label">key</span>value</p>
How am I able to get just the "value" out using xPath? I managed to get to the element using the following expression:
//span[#class='label']/..

Try this one to get required value:
//p[span[#class='label']]/text()

You just have to use text() to get the text from the p
//span[#class='label']/../text()

Related

Xpath expression (nokogiri) to get tag's child element?

From my xml, I can get this :
<home>
<creditors>
<count>2</count>
</creditors>
</home>
OR even this :
<home>
<creditors>
<moreThan>2</moreThan>
</creditors>
</home>
Which xpath expression can I use to get "<count>2</count>" instead of getting only "2" OR to get "<moreThan>2</moreThan>" instead of getting "2" ?
This XPath,
//creditors/count
will select all count child elements of all creditors elements in the XML document.
Update per OP's request in comments for a single XPath that selects both count and moreThan elements:
This XPath,
//creditors/*[self::count or self::moreThan]
will select all count or moreThan child elements of all creditors elements in the XML document.
Assuming that your xpath expression is OK, you just need to convert the element to string:
doc.xpath("home/creditors/*").to_s
=> "<count>2</count>"
Please check with queries returning more than one element, to make sure that it's desired behaviour.

how to use regex in nokogiri xpath

div class="ydpbfddd73dsignature" >......
How do I use xpath to get whatever text comes after this tag?
I tried doing this
nokogiri_html=Nokogiri::HTML html
nokogiri_html.xpath('//div[#class="/.*signature/"]')
But it doesn't work.
You can apply below XPath:
//div[substring(#class, string-length(#class) - 8)="signature"]
which means return div node which has "signature" as last 9 characters of class name

Xpath get element above

suppose I have this structure:
<div class="a" attribute="foo">
<div class="b">
<span>Text Example</span>
</div>
</div>
In xpath, I would like to retrieve the value of the attribute "attribute" given I have the text inside: Text Example
If I use this xpath:
.//*[#class='a']//*[text()='Text Example']
It returns the element span, but I need the div.a, because I need to get the value of the attribute through Selenium WebDriver
Hey there are lot of ways by which you can figure it out.
So lets say Text Example is given, you can identify it using this text:-
//span[text()='Text Example']/../.. --> If you know its 2 level up
OR
//span[text()='Text Example']/ancestor::div[#class='a'] --> If you don't know how many level up this `div` is
Above 2 xpaths can be used if you only want to identify the element using Text Example, if you don't want to iterate through this text. There are simple ways to identify it directly:-
//div[#class='a']
From your question itself you have mentioned the answer for it
but I need the div.a,
try this
driver.findElement(By.cssSelector("div.a")).getAttribute("attribute");
use cssSelector for best result.
or else try the following xpath
//div[contains(#class, 'a')]
If you want attribute of div.a with it's descendant span which contains text something, try as below :-
driver.findElement(By.xpath("//div[#class = 'a' and descendant::span[text() = 'Text Example']]")).getAttribute("attribute");
Hope it helps..:)

xpath: how to get the specific text using xpath?

the html is like this:
<div id='id'>
<a href>abc</a>
"xyz"
</div>
I want to use xpath to get the xyz (I use it in capybara), but my xpath can't work
... //div[#id='id'].text
it returns abcxyz
how can I get it?
Text is its own text node, so the correct selector would be:
.//div[#id='id']/text()

How to get element without attributes by XPath

I want to get an td element without attributes.
For example:
My code:
<td class="yyy">1234</td>
<td>5678</td>
I want to get: 5678
What the XPath for that?
Thank,
Chani
I think this is a duplicate of several other SO questions:
See this:
XPath: How to select nodes which have no attributes?
Which recommends:
//node[not(#*)]
Where node is your nodename.
try the following
/td[not(#class)]
how about
.//td[. = '5678']
or
.//td[text() = '5678']
--
if it is important that there are no attributes then,
.//td[text() = '5678' and not(#*)]
--
or, if you want to get the inner text of the first td with no attributes.
.//td[not(#*)][1]/text()

Resources