2

I am working in a ERP where the elements id are changing/rotating randomly. so if I am writing a code to locate a id, next time I run the selenium code it gets me error as the id changes by that time

How to capture the ids everytime

0

4 Answers 4

5

Sadly, it sounds like using the ID is not an option in this scenario

A lot of websites implement security to prevent automating. While this makes your job more difficult it does improve the security of the application. So what can be done to bypass these security tricks?

Use a different unique constant for that element

This could mean using the class, xpath, class and xpath, or a massive variety of other identifiers in an attempt to identify the element you are wanting. This is a lot easier than you might think. A good example would be to create a list of all links, narrow down the list of links based on their classes and then iterate through the remaining list until it contains the text you are looking for.

Use a different method than UI Automation

Depending on what you are attempting to accomplish with the testing (Does the button work or does the process work?) you could always bypass the UI as a whole and just use HTTP requests to simulate the users. Some applications this might be impossible to do but it is typically speaking a lot faster, smoother and far less maintenance work. Also, it is very simple to record HTTP using applications like fiddler.

Have adjustable security settings within the configuration

This can be a massive pain for developers but it is extremely useful feature for secured websites. By implementing a configuration that optimizes the site for automation it will allow the automation team to easily and more efficiently build their scripts. However this causes drawbacks in the sense that it is a possible security issue and convincing people the need for this might be an overwhelming, drawn out task that ends up getting done quite poorly.

And a cheat sheet to help

This cheat sheet will assist in locating the XPath and CSS of the elements. Depending on the browser you should typically choose CSS over XPath (Performance reasons) if ID and such is not an option. This especially applies to IE.

TL;DR

You have many options just not that many good ones. It will increase the processing power required to performed your duties and likely slow down testing (Unless you bypass the UI which will be more efficient). However, it is very possible.

2

You can't capture the id's every time, you need to find some other way of uniquely identifying the elements. I'm assuming this is a 3rd party tool that you don't have any control over, but if you do have control you could ask the developers to provide either a unique ID or some other unique identifier.

Some other options you have would be to identify elements using other attributes such as class, name, title, etc. You could also get all elements by tagname and then iterate through the list looking for an element that matches the inner text, or even just by index. There are a number of options using either xpath or css selectors to uniquely identify elements, depending on what the html looks like.

If you want to provide some sample html, I or others could probably help you create an identifier for whatever element you are working with.

1
  • I agree with Sam, you can try using xpath or css selector instead of the id! Sep 29, 2014 at 9:09
2

Use another identifier to identify the object or use xpath, for example:

starts-with

If your dynamic element's ids have the format where button id="continue-12345" where 12345 is a dynamic number you could use the following

XPath: //button[starts-with(@id, 'continue-')]

contains

Sometimes an element gets identified by a value that could be surrounded by other text, then contains function can be used.

XPath: //input[contains(@class, 'suggest')]

0

The image processing based Sikuli works on optical recognition of screen elements rather than HTML elements and can be a method to get around those peskily changing IDs.

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