Showing posts with label XPATH. Show all posts
Showing posts with label XPATH. Show all posts

XPATH Selectors Cheat Sheet

This article presents various XPATH expressions for finding web elements.

It uses elements from this page:

https://vpl.bibliocommons.com/search?q=java&t=keyword


The list of XPATH expressions is provided as help for learning how XPATH works.


The list is not complete so if you have any ideas of new expressions, please leave them in the Comments section.


You can test the XPATH expressions in Chrome by

  • click the Chrome menu
  • select More Tools
  • click the CONSOLE tab
  • type $x("xpath expression")
  • press ENTER


Find Elements Based on Tag


EXAMPLE

//input

Finds the elements with the INPUT tag.









Find Elements Based on Tag and an Attribute

EXAMPLE

//inpu­t[@­class='search_button']

Finds the elements with INPUT tag that have a CLASS attribute with the "­search_button­" value






Find Elements Based On 2 Attributes


EXAMPLE

//inpu­t[@­id=­'gl­oba­lQuery' and @name='q']

Finds the elements with the INPUT tag that 

1. have an ID attribute with the "­glo­bal­Que­ry" value

2. have a NAME attribute with the "­q" value







Find Elements That are Direct Children Of Another Element

EXAMPLE

//div[@class='dropdown']/a

1. finds the elements with the DIV tag that have a CLASS attribute with 
the "­dropdown­" value

2. for each element from the DIV list, finds the A elements that are direct children








Find A Specific Element From a List of Elements


EXAMPLE

(//span[@class='title'])[3]


1. finds the elements with the SPAN tag that have the CLASS attrib­ute's value equal to 
"­title"

2. selects the 3rd element from the list of SPAN elements








Find the Last Element From a List of Elements



EXAMPLE

(//span[@class='title'])[la­st()]

This example uses an XPATH function (last()).

1. finds the elements with the SPAN tag that have the CLASS attrib­ute's value equal to 
"title"

2. select the last element from the list of SPAN elements







Finds Elements That Are Included in Other Elements


EXAMPLE

(//div[@class='row top_info list_item_section'])[1]//span


1. finds the elements with the DIV tag that have the CLASS attrib­ute's value equal to "­row top_info list_item_section"

2. gets the first element from the DIV elements list

3. finds all SPAN elements (direct children or not) that are inside of the first DIV







Find Elements That Include Keywords in an Attribute



EXAMPLE

//a[co­nta­ins­(@href, 'show_­cir­cul­ati­on')]

Finds the A elements that have "­sho­w_c­irc­ula­tio­n" included in the value of the HREF attribute.

This example uses an XPATH function (contains()).








Find Elements That Have an Attribute Starting  With Keyword

EXAMPLE

//a[st­art­s-w­ith­(@href, '/item­/sh­ow_­cir­cul­ati­on')]

Finds the A elements that have the HREF attrib­ute's value starting with "­/it­em/­sho­w_c­irc­ula­tio­n".

This example uses an XPATH function (starts-with()).







Find Elements That Do Not Have A Value For Attribute

EXAMPLE

//inpu­t[n­ot(­@na­me=­'q')]

Finds the INPUT elements that have the NAME attrib­ute's value different than "­q".

This example uses an XPATH function (not()).









Get the Count of Elements Matched by an Expression

EXAMPLE

count(­//span[­@cl­ass­='title'])

Provides the count of all SPAN elements that have the CLASS attrib­ute's value equal to "title".

This example uses an XPATH function (count()).







Get An Element's Attribute's Value

EXAMPLE

//input[@testid='field_search']/@id

1. Finds the INPUT elements that have the TESTID attrib­ute's value equal to 
"­field_search"

2. Gets the value of the ID attribute for the INPUT elements








Get an Element's Value


EXAMPLE

(//span[@class='subTitle'])[1]/text()

This example uses an XPATH function (text()).

1. finds the first SPAN element that has the CLASS attrib­ute's value equal to "­subTitle"

2. gets the value of the element







Gets Links With URL Greater Than 30 Characters


EXAMPLE

//a[st­rin­g-l­eng­th(­@href) > 30]

Finds the elements with the A tag that have the HREF attribute value length greater than 30 characters.

This example uses an XPATH function (string-length()).







NEXT


Find Page Elements In Chrome With XPATH

Sometimes, the test automation of a site needs to be done in Chrome instead of Firefox.

This may be because the site works better (or only) in Chrome or because the site needs to be tested on multiple browsers.

How can site elements be found with XPATH in Chrome?

Firefox has the FireBug and FirePath plugins that help greatly with the XPATH expressions.

But there are no good equivalent browser extensions for Chrome (or I have not found them yet).

So what to do in Chrome?

The Chrome Developer Tools are the solution.

See below the steps to take:

1. click on the browser menu 
2. click Tools
3. click Developer Tools



4. click the CONSOLE tab



5. type the following in the Console tab:

$x("xpath expression")




Enjoy!



_____________________________________________________________________________


Do you want to learn more about test automation and Java?
I will start an SELENIUM online group training on October 15.
Please see the training details here.

Build different XPATH expressions for the same HTML element

One of the difficult things to learn for testers that are new in test automation is selecting HTML elements in XPATH.

In order to click, select, type text in HTML elements, they need to be found first.

I have created a few different expressions that identify the same element of a page.

This is how to find the element matched in the next examples:

1. open www.vpl.ca

2. search for cats

3. identify the title link for the 7th result

Please click on the images from below to see them full screen.



//div[@id='bib1921954038']//span[@class='title']/a





/html/body/div[5]/div[7]/div/div/div[3]/form/div[3]/div/div[8]/div[2]/div/span[@class='title']/a




(//span[@class='title']/a)[last()-2]






(//span[@class='title']/a)[8]




//a[contains(@title, 'Book') and contains(@href, '1921954038_cats')]




All expressions do the same thing in different ways.

They are presented only to highlight the flexibility and power of XPATH.

From all 5 example expressions, the only one that I would use in my projects is the 3rd one.




_____________________________________________________________________________


Do you want to learn more about test automation and Java?
I will start an SELENIUM online group training on October 15.
Please see the training details here.

Find HTML Elements With XPATH

NEXT - Selenium Basics 6 - Annotations, Commands And Assertions



VIDEO - Find HTML elements using XPATH 1

VIDEO - Find HTML elements using XPATH 2

This post is dedicated to XPATH which will be used for finding HTML elements in the browser DOM.

SELENIUM has one more way for finding HTML elements called CSS Selector.

The following will be investigated for XPATH:

- the structure of the browser DOM

- element tags, attributes and values

- finding elements using the absolute path

- searching for elements using attribute values

- searching for elements using XPATH functions

- XPATH locators that return more than one result