(Download Transcript) XPath Functions-1
(Download Transcript) XPath Functions-1
Table of Contents
Introduction ............................................................................................................................................ 2
XPath Functions....................................................................................................................................... 2
Introduction
The next AUT is GitHub. Let’s inspect the Note below Username. Here is a special case where XPath or
CSS Selector is our only option and a perfect example, to show you why we should not use Chrome
DevTools to copy XPath. Copying an XPath value works sometimes but it is not reliable. Therefore, it’s
best not to Right click, Copy, and Copy XPath then Paste XPath. Notice the id value. Now, I am going to
reload the page. Copy XPath again then paste XPath. Do you see how the value changed after reloading
the page? That is called a dynamic id value where the value changes every time. This video will show you
how to handle dynamic values using XPath Functions.
XPath Functions
There are 4 kinds of XPath Functions. On Oracle’s site. We see Node Set, String, Boolean, and Number.
Our focus will be the String Functions. In our cheat sheet, I call them Partial Pattern Match because all
we need to do is match part of the Attribute’s Value. I was going to demo 3 functions but I will remove
ends-with then demo starts-with and contains.
Back to our AUT for the starts-with function. We write 2 forward slashes, Tag Name is p, opening and
closing brackets, starts-with, opening and closing parenthesis, at symbol, the Attribute Name is id,
comma, 2 single quotes, then write what the value starts with. The value starts with description so let’s
write description between both quotes. Bingo, there it is. We can also replace starts-with and use the
next function contains. Both functions will work because the value starts with description and also
contains description. Remove starts-with then add contains. Did you notice, that we have found 1 of 2?
1 of 2 is not good enough if we are looking to only verify the note below Username. Both elements
almost look the same. How do we find 1 of 1 when both elements have p as the tagName, class attribute
with a value of note, and a dynamic id value?
Recall from the previous session, we used XPath with a Logical AND operator. Let’s look at both
WebElements. One note refers to the username and the other note refers to the email address. We can
use a word or we can use a phrase from the note. Inside the brackets, we write and contains, 2
parentheses, we write a dot, comma, 2 single quotes, within the quotes, we can write the complete
phrase This will be your username and so forth. Now, we have 1 of 1. However, I prefer to use a unique
word like username and we still have 1 of 1. This query is asking to find “a p tag that starts with an id
attribute consisting of a description value and contains text username”. We can also replace the dot
with the text function. However, there are cases where text will not work. I will create a video explaining
the difference between using an XPath value using a dot and using text. We still see 1 of 1. If you choose
not to write the word and, you can replace it with the square brackets.
Let’s go to Tools QA and continue with a different syntax for the text function. Inspect First name colon.
We see a strong tag name but don’t see any attributes. Write 2 forward slashes, strong, both brackets,
text, 2 parentheses, equal, 2 single quotes, First name colon, and bingo. We found our element. That’s it
for XPath Functions. You can download the Cheat Sheet and Transcript from github at
RexJonesII/Selenium4Beginners and https://tinyurl.com/SeleniumLocatorsForWebElements.