Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Advanced Xpath Concept – Method normalize-space & Its Usage | Make Selenium Easy

Posted on 10/05/2018 By admin

Hello Folks,

In this post we will going to learn an advanced concept of xpath: – normalize-space method. 

Before we discuss about method normalize-space, we will see why do we require that.

Consider below html codes:

   Make       Selenium      Easy (Inner text has more white spaces among words than normal.) 

It will be displaying correctly at Front End (FE) but in DOM, it is in different way i.e. containing uneven white spaces among words.

    Make Selenium Easy (Inner text has spaces in beginning and end.) 

Innex text has spaces in beginning and end of inner text. You may just need to trim those white spaces.

The same can be the case with attribute as well as shown below:

All above examples have spaces and you may need to locate them by removing unwanted white spaces. Obviously we have a method named “contains” but it will not work if there are more than one white spaces between words.  Here, normalize-space method will be useful.

Let’s see some examples below:

Scenario 1: Text or attribute value with white spaces in beginning and end:

 Text with trailing whitespaces 

Text or InnerText:

In above code, you can see inner text of tag ‘a’ has white spaces in the beginning and end. We can use contains here as below:

XPath using contains method:

//a[contains(text(),’Text with trailing whitespaces’)]

The same can be achieved using normalize-space method:

//a[normalize-space(text())=’Text with trailing whitespaces’]

Attribute Value:

XPath using contains method:

//a[contains(@id,’someAnchor’)]

The same can be achieved using normalize-space method:

//a[normalize-space(@id)=’someAnchor’]

Scenario 2: Text or attribute value with uneven white spaces in between of words:

 Text    with    uneven    whitespaces   

You can see in UI, it displays correctly but html contains uneven white spaces.

In this case, contains will not work. We need to use normalize-space method because it just not trim white spaces from beginning and end of string, also between words as well.

//a[normalize-space(text())=’Text with uneven whitespaces’]

Scenario 3: Nested elements with inner Text

Consider below nested element with inner text:

some label
some span

You can see label tag has innertext as “some label” and span tag has innertext as “some span”. First outer element has innertext followed by inner element innertext.

You can easily locate it using contains as below:

//label[contains(text(),’some label’)]

But problem happens when outer element innertext comes after inner element innertext. Example:

some span
some label 

In this case contains method will not work. You need to use normalize-space method. But here syntax will be little different as :

//label[text()[normalize-space() = ‘some label’]]

Hope you learnt something new and will be useful in locating elements.

If you have any doubt, feel free to comment below. If you like my posts, please like, comment, share and subscribe. #ThanksForReading

#HappySelenium

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Uncategorized

Post navigation

Previous Post: JavaScript Way To Handle Calendar In Selenium | Make Selenium Easy
Next Post: Object Repository in Selenium Using Page Factory With Inner Class – Handling a Page Which Has Many Webelements | Make Selenium Easy

Related Posts

invalidClassName – Make Selenium Easy Uncategorized
interview java programs Uncategorized
Understand Defect Life Cycle In Testing Uncategorized
REST Assured Tutorial 40 – @JsonIgnoreProperties Annotation Of Jackson API – Exclude Field Of Pojo From Serialization Or Deserialization or Both Uncategorized
Make Selenium Easy – Page 2 of 57 – Uncategorized
chrome Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark