HomeSelenium

Selenium Locators – starts-with() XPath Function

Selenium Locators – starts-with() XPath Function

<<Previous Post                            << Complete Tutorial>>                              Next Post>

In the previous article, I have practically demonstrated contains() XPath Function.

In this article, I am going to explain the next XPath Function known as starts-with()

Difference between contains() and starts-with() XPath Functions

While contains() can be used with any partial portion of the text or any partial portion of the attribute value. The partial portion can be at the beginning, middle or end of the text or attribute value.

Whereas starts-with() can be used with the beginning portion of the text or beginning portion of the attribute value.

Let’s get started with practical demonstrations of starts-with() XPath Function.

Selenium Locators – starts-with() XPath Function

Follow the below steps for practicing the starts-with() XPath Function:

1) Open http://omayo.blogspot.com/ in Chrome Browser, where ChroPath is already installed (Refer ChroPath installation in Chrome Browser here and its usage in Chrome Browser here ) as shown below:

Relative XPath - More complex application

2) Right click on the below text element, select ‘Inspect’ option and observe that the below HTML code got highlighted on inspection, and also observe that there is some text between the start and end tags as shown below:

starts-with() XPath Function - PraciceAutomationHere Text

From the above-hightlighted text, let’s take the beginning portion say ‘Practice’ and build the below Relative XPath Expression:

//p[starts-with(text(),’Practice)]

Let’s execute the above Relative XPath Expression having starts-with() and text() functions in ChroPath and observe that we are able to locate the UI element using the partial beginning portion of the text as shown below:

starts-with() XPath Function - located

We can also use the dot (.) instead of text() function in the above XPath Expression as shown below:

//p[starts-with(.,’Practice)]

Let’s execute the above Relative XPath Expression having starts-with() and dot in ChroPath and observe that we are able to locate the UI element using the partial text as shown below:

starts-with() XPath Function - located using dot

Hence we are able to locate the UI elements on their page using their partial beggining text by using starts-with() in XPath Expressions.

3) As already mentioned at the beginning of this article, starts-with() not only locates the UI elements using their partial beginning text, but also using the partial beggining values assigned to the attributes of HTML tags.

Let me demonstrate this with an example.

First, let’s inspect the below Checkbox UI element on the page and observe that the HTML for the checkbox got highlighted as shown below:

Selenium Locators - Checkbox HTML code

From the above-highlighed HTML code, let’s select the value=’orange’ attribute & its value.

Now, out of the above orange text, lets only take the partial beginning text ‘or’ and create the Relative XPath Expression using the starts-with() function as shown below:

//input[starts-with(@value,‘or’)]

Let’s execute the above Relative XPath Expression having starts-with() and observe that we are able to locate the UI element using the partial beginning attribute value text as shown below:

//input[starts-with(@value,’or‘)]

Let’s execute the above Relative XPath Expression having starts-with() and observe that we are able to locate the UI element using the partial beginning attribute value text as shown below:

starts-with() XPath Function - Attribute Located

Hence we are also able to locate the UI elements using their partial beginning values of HTML attribute inside tags by using starts-with() function.

4) Till now we have understood, how the starts-with() function can be used to locate the UI elements using their partial beginning text or partial attribute beginning values.

Now, I am going to explain the actual purpose of starts-with() function in real time Selenium Automation cases.

There may be some situation where we won’t be able to locate the UI elements on the page as the other than the beginning portion of their attribute values changes dynamically.

For example: In the id=’QAFox123456‘, the middle & after numbers are dynamically changing to something say id=’QAFox789012′.

We can simply create a Relative XPath Expression using starts-with() function as //tagName[starts-with(@id,’QAFox’)]

Though I cannot replicate the above purpose and example in a real-time web application now, we can simply use the above strategy to deal with such dynamical changing things in real time applications.

Here concludes this article.

In the next article, I will practically demonstrate the starts-with() XPath function.

Next Steps:

  • > To learn more about Selenium, continue to the next post (Click on Next Post link below)
  • > Check complete Selenium Tutorial Contents here (Click here)

Please leave your questions/comments/feedback below.

Happy Learning 🙂

About Me > Arun Motoori

On a mission to contribute to the Software Testing Community in all possible ways.

<<Previous Post                            << Complete Tutorial>>                              Next Post>

If you like this article, share it on LinkedIn using the below button:

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *