locating elements by id in selenium

Selenium, world’s most widely used test automation tool is highly user-friendly and easy to learn. Along with many features, the easiness to use it has made it the world’s most favoured and preferred test automation tool for testing web-based applications.

A software application should undergo various types of tests to decide the overall quality under test, finding elements to do some action is necessary. Locating elements in selenium is an easy process and there are different methods to find elements in Selenium.

Locating Elements in Selenium

Locating an element in a web page is one of the most important steps in the testing of web-based applications and Selenium offers different strategies for that.

  • Locating Elements by ID
  • Locating Element by Name
  • Locating Element by Link Text
  • Locating Element by Partial Link Text
  • Locating Element by Xpath
  • Locating Element by Class Name
  • Locating Element by Tag Name
  • Locating Element by CSS Selector.

Of these many strategies, locating elements by ID is the most useful one and the easiest one to locate an element in a web page. As you are already aware, each element will have a unique ID hence, there will be no confusion in locating the element. When you go with Class Name, Name, Tag name etc there are chances for more than one element to be present in the web page with similar or almost similar names.

A “No Such Element Exception” will be raised if no element has been found in the web page under test with the specific ID that is mentioned. However, you should know the ID of each element to use this strategy.

Example for Locating Elements by ID

Below is an apt example for locating elements by ID. Here we are locating the form element on the given web page.

<html>
<body>
 <form id=”loginForm”>
  <input name=”username” type=”text” />
  <input name=”password” type=”password” />
  <input name=”continue” type=”submit” value=”Login” />
 </form>
</body>
<html>

 

Leave a Reply

Your email address will not be published. Required fields are marked *