Locators in Selenium are an important part of web automation testing. Moreover, a locator is a method of identifying a web element on a web page. Selenium offers different locators to assist one in finding elements on a web page. Moreover, Locators are utilized in interacting with web page elements like checkboxes, buttons, & text fields. Let’s have a detailed look at the various varieties of locators in Selenium.

  • ID Locator

The ID locator is one of the most commonly utilized locators in Selenium. All the element on a web page comes with a distinct ID that could be utilized to locate it. This ID is found by inspecting the element with the help of a browser’s developer tools. In addition, the ID locator is utilized as follows:

driver.find_element_by_id('element_id')

This code finds the element with the stated ID.

  • Name Locator

This is another common locator utilized in Selenium. Every HTML tag could have a name attribute, and the attribute is utilized in identifying the element. The name locator is utilized as follows:

driver.find_element_by_name('element_name')

This code finds the element with the stated name.

  • Class Name Locator

The locator is utilized in identifying elements by their class attribute. Moreover, elements with a similar class attribute are grouped together. The class name locator is utilized as follows:

driver.find_element_by_class_name('class_name')

This code finds the element with the stated class name.

  • CSS Selector Locator

CSS selectors are a great way to find elements on a web page. They’re utilized to match HTML elements depending on their attributes. Moreover, a CSS selector can be utilized to choose elements by tag name, class, ID, and other attributes. The CSS selector locator is utilized as follows:

driver.find_element_by_css_selector('css_selector')

This code finds the element with the stated CSS selector.

  • XPATH Locator

XPath is a language utilized for locating nodes in an XML document. This can be utilized to find elements on a web page as well. XPath locator is utilized as follows:

driver.find_element_by_xpath('xpath_expression')

This code finds the element with the stated XPath expression.

  • Link Text Locator

The link text locator is utilized to find elements with an anchor tag. An anchor tag is utilized to create hyperlinks on a web page. The link text locator is utilized as follows:

driver.find_element_by_link_text('link_text')

This code finds the element with the stated link text.

  • Partial Link Text Locator

A partial link text locator is utilized to locate elements with an anchor tag through a partial text. The partial link text locator is utilized as follows:

driver.find_element_by_partial_link_text('partial_link_text')

This code finds the element with specified partial link text.

  • Tag Name Locator

The tag name locator is utilized to find elements by their tag name. Every HTML element comes with a tag name, like “div” or “p”. In addition, the tag name locator is utilized as follows:

driver.find_element_by_tag_name('tag_name')

This code finds the element with the stated tag name.

Conclusion

Locators are an important part of web automation testing. There are various types of locators in Selenium. By utilizing these locators, you can comfortably locate plus interact with elements on a web page. Every locator has its weaknesses and strengths, and you should select the one which best suits your requirement.

Share.

Terry White is a professional technical writer, WordPress developer, Web Designer, Software Engineer, and Blogger. He strives for pixel-perfect design, clean robust code, and a user-friendly interface. If you have a project in mind and like his work, feel free to contact him

Leave A Reply