xpath: how to express text nodes? - xpath

consider:
text 1
text 2
text 3
how can you express the textnode in xpath ?

As far as i know , all text in a node are collectively grouped up as a single text node. They are not hierarchical so there wont be more than 1 text node in any single element.
You might need to parse the text yourself using an xpath string function

Related

XPATH - how to get the text if an element contains a certain class

JHow do I grab this text here?
I am trying to grab the text here based on that the href contains "#faq-default".
I tried this first of all but it doesn't grab the text, only the actual href name, which is pointless:
//a/#href[contains(., '#faq-default-2')]
There will be many of these hrefs, such as default-2, default-3 so I need to do some kind of contains query, I'd guess?
You are selecting the #href node value instead of the a element value. So try this instead:
//a[contains(#href, '#faq-default-2')]

xpath - element containing exact text, but minus sibling elements?

Without using index specificity. I'm trying to target an element with exact text, but which also ignores the text of sibling elements. For example, target the span with Save below.
<span>Click and save money!</span>
<span>
<i>Icon</i>
Save
</span>
So something like //span[contains(text(), 'Save')] would grab any span with "Save" in it.
Try the xpath : //span[text()[normalize-space(.)='Save']]
It looks for span elements which have text nodes whose space-trimmed value is exactly Save

How to get all inner texts of tag by XPATH?

Is it possible to get all inner texts of some tag by XPath?
For example, in one case, there could be text: root.xpath('//h2[text()="Description"]/following-sibling::p/span/span/text())
In another case, it could be in first span: root.xpath('//h2[text()="Description"]/following-sibling::p/span/text())
So my question is, whether is there some way how to get all texts in one tag but not only on first level.
Something like root.xpath('//h2[text()="Description"]/following-sibling::p/*/text())
How about using // axis ?
//h2[text()="Description"]/following-sibling::p/span//text()
This should return all text nodes, anywhere within the span

XPath intersection of two sets

I need to extract all links from a html document having text as the inner element and not a reference to an image. Basically I would like to do a doc.select("//a/attribute::href") for all elements in a tree where doc.select("//a/text()") returns anything. Thanks!
Well you can write conditions in XPath in a predicate in square brackets, e.g. //a[text()]/#href selects the href attributes of all link (a) elements that have at least one text node child. Or if you want to make sure there is no img child element in the link you can use e.g. //a[not(img)]/#href.

help needed with xpath ...please

I'm trying to write xpath for one of the elements in a tree-like structure in the UI.
The tree looks like a windows file structure, like parent node, child. So, in order to find child node parent node has to be clicked.
+ [file icon] Book
|_ Book 1
|_ Book 2
|_ Book 3
Selenium gives the following xpath for the text 'Book' in above tree shown
//ul[#id='book_xxx']/li/ul/li[8]/span
when I click on file icon, selenium gave me following
//ul[#id='book_xxx']/li/ul/li[8]/img[1]
How can one write an xpath for clicking the file icon(i.e image) based on knowing the span text? I need the xpath for clicking on file icon image.
It appears that the span and img elements are both children of the 8th li element, and I'm assuming the text inside the span that you would like to match on is "Book".
If you wanted to select the span filtering on the text you should be able to use:
//ul[#id='book_xxx']/li/ul/li[span[text()='Book']]/img[1]
This uses a nested predicate filter to identify the li that has a child span element who's text node value is "Book", and then selects that li's first img element.
Try:
//ul[#id='book_xxx']/li/ul/li[8]/img[1] | //ul[#id='book_xxx']/li/ul/li[8]/span
Which is practically xpath1 or xpath.

Resources