HOW TO HANDLE THE WEB ELEMENTS WITH COMMON LOCATORS OR WHEN IDS ARE SAME FOR ALL WEB ELEMENTS?




Let us consider a scenario, there are 4 login buttons on page
with same id or ids are changing dynamically.

For the above scenario HTML code should be like this:

<input type="submit" id="button" value="Login" />
<input type="submit" id="button" value="Login" />
<input type="submit" id="button" value="Login" />
<input type="submit" id="button" value="Login" />




We need to use xpath as
//input[@id='button'][1]
for first login button and
//input[@id='button'][2]
for second login button.
Similarly for other login buttons.


We can also take same scenario in other way like there are all fields with common locators.

Locator for login is
id=user
for password is
id=pass
and for submit is
id=login




For the above scenario HTML code should be like this:

<div class="col1 left">
<FORM action="home.php" method="post">
<LABEL for="Username">Username </LABEL>
<INPUT type="text" id="user"><BR />
<LABEL for="Password">Password </LABEL>
<INPUT type="text" id="pass"><BR />
<INPUT type="submit" value="login" id=login>
</FORM>
</div>
<div class="col1 right">
<FORM action="home.php" method="post">
<LABEL for="Username">Username </LABEL>
<INPUT type="text" id="user"><BR />
<LABEL for="Password">Password </LABEL>
<INPUT type="text" id="pass"><BR />
<INPUT type="submit" value="login" id=login>
</FORM>
</div>
<div class="col2 left">
<FORM action="home.php" method="post">
<LABEL for="Username">Username </LABEL>
<INPUT type="text" id="user"><BR />
<LABEL for="Password">Password </LABEL>
<INPUT type="text" id="pass"><BR />
<INPUT type="submit" value="login" id=login>
</FORM>
</div>
<div class="col2 right">
<FORM action="home.php" method="post">
<LABEL for="Username">Username </LABEL>
<INPUT type="text" id="user"><BR />
<LABEL for="Password">Password </LABEL>
<INPUT type="text" id="pass"><BR />
<INPUT type="submit" value="login" id=login>
</FORM>
</div>

We need to use xpath as
//input[@id='user'][1]

for first username,
//input[@id='user'][2]

for second username.

Similarly for other elements like password & login button.



Post a Comment

2 Comments

  1. This kind of situation never occur while automating real applications

    ReplyDelete
    Replies
    1. Rajendra, the above code is just a sample but it is related to real time situation where you may require to handle duplicate IDs, etc locator. Duplicate ID, NAME, XPATH, etc are common things in real time scenarios as well.

      Delete


What would you like to add in my list? I look forward to reading your comments below.