top of page
ashinaarora

Locators in Selenium

HMTL web-based testing key actions are click, type, etc. The output of application automation should comparable to the actions performed by someone. The locators in selenium (the automation testing tool) act as a bridge between automation tools and HTML key elements.

Locators are the key component of any automation tool that helps in identifying Web Elements (such as text box, button, etc.) present of the web page. Document object model (DOM) contains all the web elements and we need to find the locators to perform the automation testing. Google Chrome is an good example of DOM for testing

DOM can be accessed in Google Chrome by right click and selecting inspect.

By pressing the arrow it will show the DOM elements. Locators supported by Selenium:

Locators in selenium come into action after the WebDriver is initialized and loaded the webpage to be tested. A locator enables testers to select an HTML DOM element to act of. The following locators are supported by selenium:

Different Types of Locators in Selenium

Here’s a snapshot overview of top 8 locators in Selenium:

1. By CSS ID: find_element_by_id

2. By CSS: find_element_by_class_name

3. By name: find_element_by_name

4. By Xpath: find_element_by_xpath

5. by tagName: find_element_by_tag_name()

6. By link text: find_element_by_link_text

7. By partial link text: find_element_by_partial_link_text

8. By HTML tag name: find_element_by_tag_name

While all these locators return single elements, one may use the .find_elements() method to find multiple elements. L

To use the above features, one needs to call the .find_element_by_id() method of the webdriver class. Here is the usage for it.

from selenium import webdriver driver = webdriver.Chrome('./chromedriver') driver.get("https://www.python.org") search_bar = driver.find_element_by_id("id-search-field")

If there is no DOM element with the ID that one is searching for, a NoSuchElementException is raised, which one can account for, by using a try-catch block.

Locate Elements by XPath


We will use the .find_element_by_xpath() method to locate an appropriate element in the document. The argument that the .find_element_by_xpath() method takes is the path to the element.

To find the email input field in the above HTML form example, use the following code:

email_input = driver.find_element_by_xpath("//form[input/@name='email']")

This code snippet searches for the first form element of the page. Within this form, it searches for input with the name, which equals the value email, thus narrowing down to the required element.

Next, let us try to locate the form’s first and last names input element above.

first_name = driver.find_element_by_xpath("//form[@id='loginForm']/input[1]") last_name = driver.find_element_by_xpath("//form[@id='loginForm']/input[2]")

The method first searches for a form with the ID login form and then selects the form’s first and second input elements as the first and last names.

HMTL web-based testing key actions are click, type, etc. The output of application automation should comparable to the actions performed by someone. The locators in selenium (the automation testing tool) act as a bridge between automation tools and HTML key elements.

Locators are the key component of any automation tool that helps in identifying Web Elements (such as text box, button, etc.) present of the web page. Document object model (DOM) contains all the web elements and we need to find the locators to perform the automation testing. Google Chrome is an good example of DOM for testing

DOM can be accessed in Google Chrome by right click and selecting inspect.

By pressing the arrow it will show the DOM elements.


Locators supported by Selenium:

Locators in selenium come into action after the WebDriver is initialized and loaded the webpage to be tested. A locator enables testers to select an HTML DOM element to act of. The following locators are supported by selenium:

Different Types of Locators in Selenium

Here’s a snapshot overview of top 8 locators in Selenium:

1. By CSS ID: find_element_by_id

2. By CSS: find_element_by_class_name

3. By name: find_element_by_name

4. By Xpath: find_element_by_xpath

5. by tagName: find_element_by_tag_name()

6. By link text: find_element_by_link_text

7. By partial link text: find_element_by_partial_link_text

8. By HTML tag name: find_element_by_tag_name

While all these locators return single elements, one may use the .find_elements() method to find multiple elements. L

To use the above features, one needs to call the .find_element_by_id() method of the webdriver class. Here is the usage for it.

from selenium import webdriver driver = webdriver.Chrome('./chromedriver') driver.get("https://www.python.org") search_bar = driver.find_element_by_id("id-search-field")

If there is no DOM element with the ID that one is searching for, a NoSuchElementException is raised, which one can account for, by using a try-catch block.


Tag and Class in CSS Selector

Apart from the syntax (or format) difference, the said locator is pretty much identical to the ID locator. A dot (.) is used when denoting the class attribute value rather than hash (#) in the case of class.

Syntax



Theoretically, every DOM element on a page should have a unique ID. However, in real life, one does not commonly observe this. Most elements may not have an ID or encounter two elements with the same ID. In such cases, one needs to use a different strategy to identify a DOM element uniquely.


















30 views

Recent Posts

See All
bottom of page