Latest Blog

Locating element using ID, Name, Class name, Tag, Link Text & Partial Link text

In our previous blog, we walked through different ways to locate elements on web or app. We will be looking at locating elements using Id, Class name, Name, Tag, Link text and Partial Link text, please make a note XPATH and CSS are covered separate blogs. With all the theory behind, let’s jump onto some practical to understand the concepts.

We are using the most common example used so far, yes google home page. Here are the list things you might want to do

  • Open google home page
  • Right click on Google Search button and select Inspect Element
  • The developer’s tool shows up with the complete DOM

If you take a close look we can see the complete HTML hierarchic of the web page, this remains the same for all web pages.

Identifying the element by ID:

The button ‘Google Search’ can be locate using its ID, i.e. ‘mKlEF’. Here is an example in Java driver.findElement(By.id(“element id”))

In our case this would look like: driver.findElement(By.id(“mKlEF”))

Identifying the element by Class Name:
The button ‘Google Search’ also has a class name, i.e. ‘sbico-c’. This button can be located using its class name if the attribute class name is unique. driver.findElement(By.className(“element class”))

In our case this would look like: driver.findElement(By.id(“sbico-c”))

Identifying the element by Name:
The button ‘Google Search’ has a name, i.e. ‘btnG’. This button can be located using its name if the attribute name is unique. driver.findElement(By.name(“element name”))

In our case this would look like: driver.findElement(By.name(“btnG”))

Identifying the element by Tag:
The button ‘Google Search’ can be located using its tag i.e. button. If the tag name is unique then the element can be located using this technique. driver.findElement(By.tagName(“element html tag name”))

In our case this would look like: driver.findElement(By.tagName(“button”))

Identifying the element by Link Text:
For this we might need a link on the page, lets right click on the link ‘Images’

The link ‘Images’ can be located using its link text i.e. Images. If the link text is unique then the element can be located using this technique. driver.findElement(By.link(“element link text”))

In our case this would look like: driver.findElement(By.link(“Images”))

Identifying the element by Partial Link Text:
The link ‘Images’ can be located using partial link text i.e. ‘Ima’. If the partial link text is unique then the element can be located using this technique. driver.findElement(By.partialLinkText(“element link text”))

In our case this would look like: driver.findElement(By.partialLinkText(“Ima”))

Conclusion: These were some simple ways to locate elements on web pages or app. It’s not important which technique is used to locate an element, it’ss important that the element is located uniquely in the whole webpage or app. Locating elements uniquely comes with practice and patience, hope this blog helps you.

Detailed and practical usage of XPaths and CSS selectors has been explained in our next blog.


29, October - 2021

Try it now for Free