Xpath Finder For Selenium

0 views
Skip to first unread message

Ogier Dudley

unread,
Aug 3, 2024, 5:40:49 PM8/3/24
to feasubgeround

In our test automation projects, we generally prefer to use id, name, class, etc., these locators. However, sometimes we could not find any of them in the DOM, and also sometimes locators of some elements change dynamically in the DOM. In these kinds of situations, we need to use dynamic XPath locators. These locators must be capable of locating complex and dynamically changing web elements. In the below sections, I will share with you 15 Tactics to Write Effective XPath Locators.

It is a very handy XPath Selenium locator, and sometimes it saves the life of a test automation engineer. When an attribute of an element is dynamic, you can use contains() for the constant part of the web element but also use contains() in any condition when you need.

It starts to locate elements after the given parent node. It finds the element before the following statement and sets it as the top node, and then starts to find all elements after that node. In the below example,

This is by far the best tutorial on using xpath. It clearly explains the structure of an xpath expression; it then describes the variety of usage, clearly and with great graphics.
Great work and thanks.
Kgething.

Hi James, to be frank, I do not have any clue when will they move from 1.0 to 2.0 and 3.1. This question should go to Simon Stewart.
I also found a question here: -i-use-xpath-2-0-with-firefox-and-selenium

As I understand the problem well, maybe we can create a method kind of utility method which has tablename parameter, and inside the code, based on the scenario, you can call that method with a specific table name argument.

The code works as expected but how does it find the table name(printer) without mentioning table name in the step definition and logical code?
then I should navigate to phones table
then i should navigate to TV table
all using the same method.

Hi Elmar, as far as I see, this implementation is fine, and if I write the test with a dynamic table name, I may use the same approach. Here as I understand, you have many tables, and you need to distinguish each of them in the automation. You may either use enums, lists, etc. but somehow you need to mention to the XPath line which table you will use. If not, I am unsure how the XPath logic understands which table you want to work on. There are some workarounds like putting all table names in a collection like an array or list, and in the step file, you will iterate each of them until they find logic, find the element, etc., but IMO this implementation will be uglier than yours.

HI
I like your tutorial.
I am working on a automation i am unable to find xpath. Its there. but when i find its not coming. I have tried all what you have teach here.
i can find html web element, i can find /body and then the third one is macroponent-f51912f4c700201072b211d4d8c26010 . i can find this too. but after that, even i try relative or absolute path, next div it says its not there.

c80f0f1006
Reply all
Reply to author
Forward
0 new messages