Xpath Parent Attribute of Selection

xpath parent attribute of selection

Instead of traversing back to the parent, just find the right parent to begin with:

  • //x will select all x elements.
  • //x[//z] will select all x elements which have z elements as descendants.
  • //x[//z]/@name will get the name attribute of each of those elements.

Select by attribute of parent node in xpath query

Try the following :

allrows = table.find_elements_by_xpath("//tr[@class='Leaguestitle' or contains(@id,'tr1') and not (@align='center')]")

XPath: Get parent node from child node

Use the parent axes with the parent node's name.

//*[title="50"]/parent::store

This XPath will only select the parent node if it is a store.

But you can also use one of these

//*[title="50"]/parent::*
//*[title="50"]/..

These xpaths will select any parent node. So if the document changes you will always select a node, even if it is not the node you expect.

EDIT

What happens in the given example where the parent is a bicycle but the parent of the parent is a store?

Does it ascent?

No, it only selects the store if it is a parent of the node that matches //*[title="50"].

If not, is there a method to ascent in such cases and return None if there is no such parent?

Yes, you can use ancestor axes

//*[title="50"]/ancestor::store

This will select all ancestors of the node matching //*[title="50"] that are ` stores. E.g.

<data xmlns:d="defiant-namespace" d:mi="23">
<store mi="1">
<store mi="22">
<book price="8.95" d:price="Number" d:mi="13">
<title d:constr="String" d:mi="10">50</title>
<category d:constr="String" d:mi="11">reference</category>
<author d:constr="String" d:mi="12">Nigel Rees</author>
</book>
</store>
</store>
</data>

XPath selection result

XPath select parent attribute and child element data

This xpath 2.0 expression (and properly closing your sample html)

//parent/concat(@attr1,' ',child3/text())

should output:

a11 c13
a21 c23

Selecting a parent node in XPath?

Parent can be selected via .. in XPath, but often it's cleaner to use predicates higher in the ancestry instead:

//Item[not(ItemAttributes/Author="J.K. Rowling")]/ASIN

will select all ASIN elements in Item elements that do not have an ItemAttributes/Author with string value of "J.K. Rowling".

XPath for attribute in parent not containing a specific character?

This XPath,

//child[not(contains(../@attr, "Y"))]

will select all child elements that do not have a parent with an attr attribute value that contains a "Y",

<child id="child_11"/>
<child id="child_12"/>

as requested.

XPath Select all children with specific parent node by attribute

For the div element with an id attribute of hero //div[@id='hero'], these XPath expression will select elements as follows:

  • //div[@id='hero']/* will select all of its children elements.
  • //div[@id='hero']/img will select all of its children img elements.
  • //div[@id='hero']//* will select all of its descendent elements.
  • //div[@id='hero']//img will select all of its descendent img elements.

How select a parent node with XPath?

You can use either of the below options.

//td[@value="val"]/ancestor::a
^
td with value val
^
ancestor link

or

Preferred xpath in this case

//a[.//td[@value="val"]]
^
Get me any link which have td with value as val.

or

The below xpath works now, but when there any change to the page eg: if table is moved into a div, then this xpath will break.

//td[@value="val"]/parent::tr/parent::table/parent::a

Personally I prefer the 2nd option atleast in this case as a does not have any specific properties. And ancestor::a will select any link which is ancestor of the td.



Related Topics



Leave a reply



Submit