Challenge: Selenium-Supported Element Locators

Test your knowledge of Selenium-supported element locators in this exercise.

We'll cover the following

Problem statement

Develop a program that demonstrates the usage of various element locators supported by Selenium. The program should include functionalities to search for elements using different locators, such as ID, name, tagName, className, linkText, partialLinkText, xpath, and cssSelector.

Tasks

  1. Utilize the predefined findElement function, which accepts two string parameters, locatorType and value, to effectively find and return web elements based on the specified locator types and values.

  2. Support the following locator types: ID, name, tagName, className, linkText, partialLinkText, xpath, and cssSelector.

  3. Handle exceptions appropriately if the element is not found.

  4. Utilize the provided driver object for finding elements.

Playground

To search for an xpath element on the packtpub.com website, use the following command:

Get hands-on with 1400+ tech skills courses.