There are multiple ways to handle this problem, as others have said. The most important thing to remember is that implementation time is important - if a potential solution will take you several days to build and be fragile, where a solution involving programmers takes a few hours from you and a few hours from the programmers and will then be extremely stable, the solution with programmers is the best option.
I'm going to list the main options in the approximate order I prefer to use them:
- Work with developers to get every element you interact with a unique identifier - It doesn't matter if the identifier is ID (although that is best because it's fastest with Selenium), but if each element you need has a known attribute that will have a unique value, it will save you an immense amount of time and be more stable than using other methods.
- Find by some aspect of the element that is unique - If the element has an attribute or inner text that is unique on the page, you can located by this attribute or inner text. This is more fragile than a designated ID field because whatever makes the part of the element unique may be something that is edited often. For instance, if an element is the only
<select> on the page, you can find by tag name - but if another <select> is added to the page, your scripts will have to be edited.
- Use DOM traversal from a unique element to your target - This method will break if the page structure changes, but you can do things like finding a sibling element with some unique factor like in point 2, then navigating to the parent element and doing a find using something unique to the target element within the parent. For a simplistic example, in the fake html snip below, if there was only a single submit element on the page and the labels on the forms weren't unique, and your target was the field2 input, you could find your target by first finding the submit field, then navigating to
submit.parent(), and finally finding the input element in the paragraph tag where text.contains("field2")
<form target="someurl.html">
<p>field1 <input type="text" /></p>
<p>field2 <input type="text" /></p>
<input type="submit" />
</form>
- Use Javascript DOM traversal - In some ways Javascript DOM traversal is easier than Selenium DOM traversal, in that you have more navigation methods to work with, but the coding to call this is more complex than just staying with the base Selenium methods. The principle is similar to method 3.
- Xpaths with page structure - This is the absolute last resort, simply because page structure can change a lot, and this method will create a maintenance nightmare, especially if your page is being dynamically altered as you interact with it. If you find that this is the only method you can use, I'd recommend you use estimated maintenance cost as an argument to convince your manager to try method 1 instead. You may have to suffer with this method for a while before you have the evidence to push method 1.
class:x-boundlist-item, Does it contains list? – Helping Hands Nov 30 '15 at 04:56Is there anyway I can click on the names of the field ?
– Som Ghosh Nov 30 '15 at 06:43