In the previous chapter of WebElement Commands, we learned different types to actions which can be performed on a WebElement object. Thus the next thing to do is to interact with a web page to use WebElement Commands/Actions. First thing to locate an element on the web page before interacting with it and locating elements can be done on the WebDriver Instance(driver) itself or on a WebElement. WebDriver gives us Find Element and Find Elements methods to locate element on the web page. This chapter talks about the same “Find Element in Selenium“.

Find Element in Selenium

As in the previous chapters, we learned that every method of the WebDriver either returns something or return void(means return nothing). The same way findElement method of WebDriver returns a WebElement.

But the findElement() method accepts something as a Parameter/Argument and which is By Object. By is the mechanism used to locate elements within a document with the help of locator value. A normal syntax of By looks like this:

Find Element in Selenium using By Strategy

Finding elements in Selenium WebDriver is done by using the findElement(By.locator()) method. The findElement methods take a locator or query object called ‘By’. In the eclipse code window type driver.findElement(By dot), Eclipse IntelliSense will populate the list of different locators. ‘By‘ strategies are listed below.

By ID

id(String id): By – This is the most efficient and preferred way to find an element in Selenium, as most of the times IDs are unique. It takes a parameter of String which is a Value of ID attribute and it returns a BY object to findElement() method.

Command– driver.findElement(By.id(“Element ID”));

With this strategy, If no element has a matching id attribute, a NoSuchElementException will be raised.

Example: If an element is given like this:

Actual Command

CSS

1

2

3

WebElementelement=driver.findElement(By.id("submit"));

//ActioncanbeperformedonInputButtonelement

element.submit();

Note: Common pitfalls that UI developers make is having non-unique id’s on a page or auto-generating the id, both should be avoided.

By Name

name(String name): By – This is also an efficient way to locate an element but again the problem is same as with ID that UI developer makes it having non-unique names on a page or auto-generating the names. It takes a parameter of String which is a Value of NAME attribute and it returns a BY object to findElement() method.

Command– driver.findElement(By.name(“Element NAME”));

With this strategy, the first element with the name attribute value matching the location will be returned. If no element has a matching name attribute, a NoSuchElementExceptionwill be raised.

Example: If an element is given like this:

Actual Command

CSS

1

2

3

WebElementelement=driver.findElement(By.name("firstname"));

//ActioncanbeperformedonInputTextelement

element.sendKeys("ToolsQA");

By ClassName

className(String className): By – This finds elements based on the value of the CLASS attribute. It takes a parameter of String which is a Value of CLASS attribute and it returns a BY object to findElement() method.

Command– driver.findElement(By.className(“Element CLASSNAME”));

If an element has many classes then this will match against each of them.

Example: If an element is given like this:

Actual Command

1

2

3

WebElement parentElement=driver.findElement(By.className("button"));

WebElement childElement=parentElement.findElement(By.id("submit"));

childElement.submit();

Note: This method is a lifesaver. As said, a class can contain many elements, many times when you end up with duplicate IDs and Names, just go for the ClassName first and try to locate the element with ID. That will work fine, as the selenium will look for the ID which is in the mentioned class.

By TagName

tagName(String name): By – With this, you can find elements by their TAGNAMES. It takes a parameter of String which is a Value of TAG attribute and it returns a BY object to findElement() method.

Command– driver.findElement(By.tagName(“Element TAGNAME”));

Locating Element By Tag Name is not too much popular because, in most of the cases, we will have other alternatives of element locators. But yes if there is not an alternative then you can use element’s DOM Tag Name to locate that element in WebDriver.

Example: If an element is given like this:

Actual Command

CSS

1

2

3

WebElementelement=driver.findElement(By.tagName("button"));

//ActioncanbeperformedonInputButtonelement

element.submit();

By LinkText & PartialLinkText

linkText(String linkText) : By – With this you can find elements of “a” tags(Link) with the link names. Use this when you know link text used within an anchor tag. It takes a parameter of String which is a Value of LINKTEXT attribute and it returns a BY object to findElement() method.

partialLinkText(String linkText) : By – With this you can find elements of “a” tags(Link) with the partial link names.

Command– driver.findElement(By.linkText(“Element LINKTEXT”));

Command– driver.findElement(By.partialLinkText(“Element LINKTEXT”));

If your targeted element is link text then you can use by link text element locator to locate that element. Partial Link Text is also same as Link text, but in this, we can locate element by partial link text too. In that case, we need to use By.partialLinkText at a place of By.linkText.

By XPath

xpath(String xpathexpression) : By – It is most popular and majorly used locating element technique or the easiest way to locate an element in WebDriver. It takes a parameter of String which is an XPATHEXPRESSION and it returns a BY object to findElement() method.

Command– driver.findElement(By.xpath(“Element XPATHEXPRESSION”));

The best thing in XPath is that it provides a much different technique to locate elements. It gives you a feature to locate a single element in many ways.

We have a complete chapter on XPath techniques which we will come across during our learning journey on ToolsQA latter.

Difference between FindElement & FindElements Commands

The difference between findElement() and findElements() method is the first returns a WebElement object otherwise it throws an exception and the latter returns a List of WebElements, it can return an empty list if no DOM elements match the query.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT

Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are as essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are as essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.