Get Locator Of Web Element.

Tram Ho

An important issue in automated testing with the Selenium project is identifying the locator of the elements on the site. The naked eye can identify elements that are buttons, check boxes, radio buttons, dropdowns, etc. However, how to get the locator must depend on the structure and composition of the site. Elements on the page will have a corresponding locator. And the element’s locator provides us with a way to gain access to the HTML elements in a web page. With Selenium we can use these locators to send requests to execute interactions to text boxes, links integrated on pages, checkboxes, buttons or other web elements.

In fact, sometimes it is not always easy to determine the position of elements on the web interface, sometimes it is not possible to retrieve the information that the element needs to retrieve, or sometimes it is impossible to retrieve. OK. The order of precedence used for locating

  • Id
  • Name
  • LinkText
  • Partial Link Text
  • Tag Name
  • CSS Class Name
  • CSS selector
  • XPath

I took locator some components in the web!

1. Id

Each element on the regular page will be given a unique id. And this is the best way to use it to get the element’s locator. For example:

2. Name

Similar to the id attribute, each data field will be given a unique name and usually this name will not be changed after upgrades or modifications. This is one of the best options when getting locators for elements in the login or form that have many similar input fields, such as a cargo declaration or a certain form register.

3. Link text

Using link text is a great way to find links within web pages, which are usually displayed as hyperlinks

4. CSS selector – Access to web elements

CSS selectors are not different from XPath, but this is considered to be more flexible and powerful than using XPath. Unlike XPath, CSS selector does not depend on DOM structure. Help you to be able to perform some interactions that when using XPath, you can hardly do. You will find it easy to find the element’s unique locator by combining it with many other CSS properties. However, it also requires users to have a deeper understanding of CSS / Javascript.

Share the news now

Source : Viblo