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]
//input[@id='button'][2]
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
id=pass
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.
2 Comments
This kind of situation never occur while automating real applications
ReplyDeleteRajendra, 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.
DeleteWhat would you like to add in my list? I look forward to reading your comments below.