1

After watching videos on dynamic x-path in Selenium I want to ask:

  • Q1: what does "dynamic" actually mean? Does it mean that the value changes at runtime or it can change over a period of time with no change to the application in between?
  • Q2: Also, when we select a dynamic x-path how can we know ahead of time which values will stay the same and which will change?
  • Q3: Lastly, can a user change any values using firebugs manually to make them unique, so he wont need to resort to finding elements dynamically?

3 Answers 3

1

dynamic - that changes

Some websites may have a backend implementation that is generating random values for the attributes of an element like class, id and others.

These values may be changed on every page load or per build, depending on the application.The random value can be the entire attribute or just a part from it,for example: id="my_account-3982896", where the number is changing.

There are some of the commerce platforms and maybe other type of platforms where you can easy set-up a web page that will generate some 'random' values/attributes for the page elements.

For these type of application the trick is to not get a selector based on an attribute that is changing but to focus on the values that are not changing or on the part of an attribute that is not changing.

For the above example to find the element that has an id that contains my_account.

The user can change the attribute of an element but it will be rewetted on reload of the page, anyway you need first to identify the right element and if you did that there is no need to set any attribute/value to make them unique.

A dynamic path can also refer to an xpath that contains a variable, for example: lets say that you want to click on the order number link from order history, you will save the order number from order success page in a variable like $orderNumber, then use this variable to identify the link that contains this number like //a[contains(text(), $orderNumber)]

1

After watching videos on dynamic x-path in Selenium I want to ask what does "dynamic" actually mean?

I don't know which videos you're referring to, so I'll guess at an answer.

Imagine someone asks for directions from the airport to your house. In most places you could simply give them the street address. Since no other house has the same street address, they could type it into Google Maps and get directions from there.

Now imagine you lived in a scene from Inception. Your street address changes from one moment to the next, so you can't use it to give directions. Instead, you might say, "Go to Heisenberg Street, then find the fifth house on the right." But we're in an Inception scene, so the street changes names too.

If you can't rely on the street name, you might try saying, "Go straight out of the airport, get on the highway, go two miles, then take the right right, go three blocks, and turn left. I'm the fifth house on the block." I didn't use any names there, just turns and and distances. That works unless the house can switch places with another house, or a highway can get longer or shorter, or extra streets can spring into existence.

All of that can happen with elements in a web page. Those changes are called dynamic elements.

Does it mean that the value changes at runtime or it can change over a period of time with no change to the application in between?

It depends. A hard part about automating UI tests is knowing where the elements are. When you refer to an element, you want to do so in terms of things that don't change. An element's value -- especially an input element -- is the most likely thing to change, so normally you would use something else about the element, like its name or ID or its relationship to other elements.

Also, when we select a dynamic x-path how can we know ahead of time which values will stay the same and which will change?

In general, you can't. You have to guess based on what you know about the website. Sometimes you will be wrong, so you will have to change the XPath. Other times, your guess will be invalidated by a change to the website. That's part of what makes automating UIs challenging.

Lastly, can a user change any values using firebugs manually to make them unique, so he wont need to resort to finding elements dynamically?

No. Changes by Firebug go away when the web page is refreshed.

1

Q1 and Q3 were sufficiently answered.

re Q2: Avoid XPath like a plague if you can. It is slow and flaky. Usually framework developers use (say Angular) uses dynamic IDs for elements. So take out developers for a pizza and explain them how important is to have a name or specific CSS class for all elements you need to interact with. CSS class are cheap, element can have multiple CSS classes and such class (used for locating elements) does not need to have any formatting associated with it - it is just an aid to locate elements.

Of course name or CSS class is not unique, so you will get array of elements. If you are lucky, the length of array is 1 and you are done. If less lucky, you can look over located elements and find out your target element by other properties. Always better that struggle with XPath.

If your developers are not responsive to need of testing and reluctant to help you by adding IDs/names//CSS classes, go to work to a company where they are: life is too short to fight with flaky XPath. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.