XPath Flashcards
Что такое нода?
XPath
- element,
- attribute,
- text,
- namespace,
- processing-instruction,
- comment,
- root nodes.
XML documents are treated as trees of nodes.
The topmost element of the tree is called the root element.
<bookstore> <book> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore>
bookstore - root node
author - element node
lang - attribute node
Выбрать все ноды bookstore
XPath
nodename
Selects all nodes with the name “nodename”
Пример: bookstore
Выбор от корневой ноды
XPath
/
- выбирает корневые ноды
Пример /bookstore
Note: If the path starts with a slash ( / ) it always represents an absolute path to an element!
Выбрать все ноды book
XPath
//
- выбирает ноды в документе от текущей ноды которые подходят по имени не смотря на положение в документе
Пример: //book
Выбрать текущую ноду
XPath
.
- выбирает текущую ноду
Выбрать родителя текущей ноды
XPath
..
- выбирает родителя текущей ноды
Выбрать атрибут
XPath
@
- выбирает атрибуты
//@lang - выбирает все атрибуты lang
Альтернатива://*[@id='Element ID']
Examples//*[@id="Element ID"]
will select any element with id=”cool”//ul[@id="long"]
will select <ul id="long">
Выбрать все дочерние элементы book ноды bookstore
XPath
bookstore/book
Выбрать всех наследников book ноды bookstore независимо от положения внутри
XPath
bookstore//book
Wildcard. Любой элемент ноды
XPath
*
- Matches any element node
/bookstore/* - выбирает все дочерние элементы ноды
//* - выбирает все элементы в документе
Wildcard. Найти все атрибуты
XPath
@*
- Matches any attribute node
//title[@*]
- выбирает все элементы title у которых есть хотябы один любой атрибут
Wildcard. Найти ноду любого типа
XPath
node() - Matches any node of any kind
Выбрать одновременно title и price внутри book
XPath
By using the '|'
operator in an XPath expression you can select several paths.
//book/title | //book/price
- Selects all the title AND price elements of all book elements
//title | //price
- Selects all the title AND price elements in the document
/bookstore/book/title | //price
- Selects all the title elements of the book element of the bookstore element AND all the price elements in the document
Select all ancestors (parent, grandparent, etc.) of the current node
XPath Axes
ancestor
ancestor::book
- Selects all book ancestors of the current node
Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself
XPath Axes
ancestor-or-self
ancestor-or-self::book
- Selects all book ancestors of the current node - and the current as well if it is a book node
Selects all attributes of the current node
XPath Axes
attribute
attribute::lang
- Selects the lang attribute of the current node
attribute::*
- Selects all attributes of the current node
Selects all children of the current node
XPath Axes
child
child::book
- Selects all book nodes that are children of the current node
child::*
- Selects all element children of the current node
child::text()
- Selects all text node children of the current node
child::node()
- Selects all children of the current node
child::*/child::price
- Selects all price grandchildren of the current node
Selects all descendants (children, grandchildren, etc.) of the current node
XPath Axes
descendant
descendant::book
- Selects all book descendants of the current node
Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself
XPath Axes
descendant-or-self
Selects everything in the document after the closing tag of the current node
XPath Axes
following
Selects all siblings after the current node
XPath Axes
following-sibling
//A/following-sibling::B
Examples//p/following-sibling::div
will select every element with <div>
that directly follows a p//div//following-sibling::a
will select every a element that directly follows a div
Selects all namespace nodes of the current node
XPath Axes
namespace
Selects the parent of the current node
XPath Axes
parent
Selects all nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes
XPath Axes
preceding
Selects all siblings before the current node
XPath Axes
preceding-sibling
//A/preceding-sibling::B
Examples//p/preceding-sibling::div
will select every element with <div>
that directly precends a p
Selects the current node
XPath Axes
self
Location Path Expression
XPath Axes
A location path can be absolute or relative.
An absolute location path starts with a slash ( / ) and a relative location path does not. In both cases the location path consists of one or more steps, each separated by a slash:
An absolute location path: /step/step/... A relative location path: step/step/...
Each step is evaluated against the nodes in the current node-set.
A step consists of:
- an axis (defines the tree-relationship between the selected nodes and the current node)
- a node-test (identifies a node within an axis)
- zero or more predicates (to further refine the selected node-set)
The syntax for a location step is:axisname::nodetest[predicate]
Select the title of the first book
/bookstore/book[1]/title
Select price nodes with price>35
/bookstore/book[price>35]/price
Select title nodes with price>35
/bookstore/book[price>35]/title
Class selector
XPath
Должно работать //*[@class, 'value']
//*[contains(@Attribute,'value')]
//*[contains(@class,"neato")]
selects all elements with class=”neato”
Index Selector
XPath
Select element at given index(A)[Index]
- counting from 0
You can select a specific element given within a xpath result.
Examples(//a)[2]
will select second A
Select last selector
XPath
Select the last element inside of another element[last()]
You can use this selector to select an element that is the last child element inside of another element.
Pro Tip → In cases where there is only one element, that element counts as the first-child, only-child and last-child!
Examples(//div)[last()]
selects the last <div>
element.//div/*[last()]
selects all the last elements within <div>
elements.
Last - N selector
XPath
Select the second last element inside of another element//A[last()-N]
You can use this selector to select an element that is the last - N child element inside of another element.
Examples(//div)[last()-3]
selects the 4th last <div>
element.
Multiple Attribute Value Selector
XPath
Select all elements that have multiple specific attribute value//B[@attribute='value' and @otherAttribute='other value']/A
Attribute selectors are case sensitive, each character must match exactly.
Examples//input[@type="text" and @placeholer="username"]
selects all text input elements with placeholder username.
Attribute Starts With Selector
Select all elements with an attribute value that starts with specific characters//*[starts-with(@attribute,"value")]
Examples//toy[starts-with(@category,"Swim")]
selects elements with class toy and category “Swimwear”
Attribute Ends With Selector
Select all elements with an attribute value that ends with specific characters. The ‘ends-with’ function is part of xpath 2.0 but browsers generally only support 1.0//*[substring(@attribute, string-length(@attribute) - string-length('end text') +1) = 'end text']
Examples//img[substring(@src, string-length(@src) - string-length('.jpg')+1 ) '.jpg' ]
selects all images display a .jpg image.
Поиск по текту
XPath
//a[.='Текст ссылки']
- точка это текстовое содержимое выбранного элемента
contains() - по части текста
//h1[contains(text(),’ Log in to Twitter’)]
starts-with() - начинается с текста//h1[starts-with(text(),’Log in’)]
text() - весь текст//h1[text()='Log in to Twitter']
Удаление лишних пробелов в строке
XPath
normalize-space()
<div class="btn">Войти через Google</div>
Плохая практика //[text()=’Войти через Google’]
Хорошая практика //[normalize-space(text())=’Войти через Google’]
Почему: Последний селектор нивелирует ошибки в верстке, связанные с пробелами. Например, это спасет в подобной ситуации:
<div class="btn"> Войти через Google </div> <div class="btn"> Войти через Google </div>
__
<a class="Menu_item\_\_5wCCC">Карта</a> //a[normalize-space(.)=’Карта’]
Использование поиска по вложенному тексту (.)
Для тегов <h1>-<h6>, <a>, <button>, <li>, <td>
по возможности необходимо использовать поиск по вложенному тексту (.), а не по text().
Код:
<button jsname="pzCKEc" class="EzVRq" aria-controls="dEjpnf" aria-haspopup="true">Настройки</button>
Плохая практика //button[normalize-space(text())='Настройки']
Хорошая практика: //button[normalize-space(.)='Настройки']
Почему: Разработчик в любой момент может обернуть текст внутри в какой-нибудь <span>, и тогда поиск по text() не будет работать. Бывает, что фреймворки не могут кликнуть по вложенному тегу, иногда это приводит к ошибкам перехвата клика элементом уровнем выше. Если же искать по вложенному тексту, все эти проблемы перестают быть опасными.</span>
Подзапросы (вместо подъема по дереву)
XPath
//form[./input[@id='test2']]
- найти такую форму, внутри которой находится input с идентификатором
Является альтернативой для //input[@id='test2']/..