WebElement in Selenium

Anything present on the webpage such as textbox, text, button, link, table, radio button, checkbox etc, are called as webelement.

web elements in selenium-webdriver

Before performing any action on webelement (typing, clicking, selecting etc ), selenium has to uniquely identify those elements.

In order to use this, it uses few of the characteristics(8 types) are given by the developer while developing the webpage using html.

How to find WebElement ?

In order to interact with UI objects with Selenium WebDriver we need to be able to identify webpage elements fast and in an accurate way(sometimes uniquely). We don’t want to refactor these locators very often so we must choose the right locators from the start.

There are some browser tools that you can use in order to identify web elements in the DOM easier.

1. Firebug for Firefox
2. Google Developer Tools for Chrome
3. Web Inspector for Safari

We will explore the above tools in following tutorials.

Locators in Selenium

Selenium uses locators to find and match the elements of the page that it needs to interact with. There are 8 locators are present in Selenium:

  • Id
  • Name
  • ClassName
  • TagName
  • LinkText
  • Partial Link text
  • CSS
  • Xpath

Below image the highlighted the locators present in selenium locators in selenium-webdriver

Apart from these 8 locators we have another 18 more inbuilt locators, Selenium api provides power to the user to create custom locators in selenium.

Read QR Code using Selenium

Priority of Locators in Selenium

Automation developer should the locator in below order, so the failures will get reduced, if id has more than one match then user should try with Name so on..

  • Id
  • Name
  • ClassName
  • CSS
  • Xpath

How to open developer tools to find elements

By doing following operation in respective browser we can open the developer tools

  • IE: Developers Tool (Tools –> Developers Tools/ F12)
  • Chrome: Build in Page analyzing feature (right click –> Inspect Element / F12)
  • Firefox: Firebug add on. Right click on any element and select Inspect Element or F12

Sample HTML for Locators Example


									<html>
										<body>
											<div id="pancakes">
												<a href="https://chercher.tech" >Selenium Webdriver</a>
												<button id="firstButton" type="button">Blueberry</button>
												<button type="button" name="Ban" class="Banana">Banana</button>
												<button type="button" name="cake" value="Strawberry">Strawberry</button>
											</div>
										</body>
									</html>
								

attributes in selenium-webdriver

Id

The Id locator looks for an element in the page having an id attribute


										import org.openqa.selenium.By;
										import org.openqa.selenium.WebDriver;
										import org.openqa.selenium.firefox.FirefoxDriver;

										public class ElementLocators {
											public static void main (String [] args){
												WebDriver driver = new FirefoxDriver();
												driver.get("https://chercher.tech/locator-example")
												driver.findElement(By.id("firstButton")).click();
											}
										}
									

Name

The name locator looks for an element in the page having an name attribute


											import org.openqa.selenium.By;
											import org.openqa.selenium.WebDriver;
											import org.openqa.selenium.firefox.FirefoxDriver;

											public class Locators {
												public static void main (String [] args){
													WebDriver driver = new FirefoxDriver();
													driver.get("https://chercher.tech/locator-example");
													// Below line matches with banana button
													driver.findElement(By.name("Ban")).click();
												}
											}
										

Class Name

The Class name locator looks for an element in the page having an class attribute classname in selenium-webdriver


												import org.openqa.selenium.By;
												import org.openqa.selenium.WebDriver;
												import org.openqa.selenium.firefox.FirefoxDriver;

												public class Locators {
													public static void main (String [] args){
														WebDriver driver = new FirefoxDriver();
														driver.get("https://chercher.tech/locator_example");
														// Below line matches with banana button
														driver.findElement(By.className(".banana")).click();
													}
												}
											

tagName

The tagname locator looks for an element in the page having an tagname, like <a>, <button>, <p>, <label>, <div> etc tagname in selenium-webdriver


													import org.openqa.selenium.By;
													import org.openqa.selenium.WebDriver;
													import org.openqa.selenium.firefox.FirefoxDriver;

													public class Locators {
														public static void main (String [] args){
															WebDriver driver = new FirefoxDriver();
															driver.get("https://chercher.tech/locator-example");
															// Below line matches with banana button
															driver.findElement(By.className(".banana")).click();
														}
													}
												

Partial Link Text

In some situations, we may need to find links by a portion of the text in a Link Text element. it contains. In such situations, we use Partial Link Text to locate elements.

linktext in selenium-webdriver


																import org.openqa.selenium.By;
																import org.openqa.selenium.WebDriver;
																import org.openqa.selenium.firefox.FirefoxDriver;

																public class Locators {
																	public static void main (String [] args){
																		WebDriver driver = new FirefoxDriver();
																		driver.get("https://chercher.tech/locator-example")
																		driver.findElement(By.partialLinkText("Selenium")).click();
																	}
																}
															

CSS Selector in Selenium

CSS Selector is the combination of an element selector and a selector value which identifies the web element within a web page. The composite of element selector and selector value is known as Selector Pattern.

When we don't have an option to choose Id or Name, we should prefer using CSS locators as the best alternative. Read how to
build CSS Selector in Selenium


																	import org.openqa.selenium.By;
																	import org.openqa.selenium.WebDriver;
																	import org.openqa.selenium.firefox.FirefoxDriver;

																	public class Locators {
																		public static void main (String [] args){
																			WebDriver driver = new FirefoxDriver();
																			driver.get("https://chercher.tech/locator-example");
																			// # means id, so here below locator points
																			// to element which have id as firstButton
																			driver.findElement(By.cssSelector("#firstButton")).click();
																		}
																	}
																

Single Inheritance in Selenium and java

Good & Reliable Selenium locator

Readability of Locator:

Shorter is better, preferable with a clear unique name/id which describes this unique element on the page.

Feel free to change the code like classes/names/id's to make the locator and the test-code very understandable and readable.

We should avoid using classes or IDs which has more than one space in its value, if the values is having more spaces indirectly means that this locators valus is about to change frequently

Maintainability of locator:

The structure of the locator should be so uniquely good that it does not need to be updated if the location of the element in the page changes.

Unless the functionality changes you should be able to minimize the updating of locators and or tests.

Speed of locator:

When it comes to locators, speed is also important aspect, when we design locators we should give preferences to Id, which are retty fast compared to other locators.

But we cannot say whether xpath is good or CSS at the speed, because the based on the browser the speed of locating an element in selenium changes.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions