Selenium WebDriver WebElement Selectors

Hello friends.. 

This would be very basic topic for experienced people, but may help WebDriver new bees.

Basically whenever we interacting with Browser Application through WebDriver, we always need to identify the web-elements like Text-Box, Buttons, List, Dropdown, Menu, Link, Checkbox.. etc.

We use FindWebElement / FindWebElements method to locate/find Web-Elements, with different selector

But I will also like to mention here that as a Web-Application Tester, we should have basic knowledge of HTML & CSS, which definitely help us to take judgement on selectors to find the Web-Elements. 

go through below links to learn HTML / CSS

Below is the list of selectors provided by WebDriver.

  • By.className
  • By.cssSelector
  • By.linkText
  • By.partialLinkText
  • By.tagName
  • By.xpath

Today i am going to explain some of the listed selectors.


This Method takes Class Name as String and Finds elements based on the value of the “class” attribute of the web element.

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

For example if the value is “one two onone”, then the following “className”  will match: “one” and “two”

Have a look at below html snippet which may present on any Web Page

	<div class="displaydiv">
		<div class="boldText">My List Items</div>
				<div class="boldText listItem">First Item</div>
				<div class="boldText listItem">Second Item</div>
				<div class="boldText listItem">Third Item</div>
				<div class="boldText listItem">Fourth Item</div>
				<div class="boldText listItem">Fifth Item</div>

If we want to select all list items from the list, by using ByClassName Selector, we should use class “listItem” because “bolderText” class is also applied to List header.

So in this case our WebDriver selector statement will look as follow 

		List listItems = driver.findElements(By.className("listItem"));


While using CssSelector we should know basics of Cascaded Style Sheets.

go through below links to learn HTML / CSS

Let’s have some basics on the css.

  •     .(dot) is used for selecting the elemnts having the specified css class
  •     #(hash) is used for selecting the element having specified Id
  •     we can directly add Tag Name to select all the specified tags in the web page.

so for above given example we are selecting element by class name so our selector statement will be

		List listItems = driver.findElements(By.cssSelector(".listItem"));

we can also use below syntax if we have the single list in whole web page

		List listItems = driver.findElements(By.cssSelector("li div"));

here selector finds div whch are chield of li directly by tag name of web element &

These two selectors are quite simple and straight forward as they selector selects the element having specified id and similarly selectors selects the web element having specified name.


This Selector used to select the anchors(Links) from the web-page.

It selects the link on the basis of the text contains by the link. consider below HTML snippet

		<a id='myLink' href=''> Link to Google</a><br/>
		<a id='myLink' href=''> Link to Yahoo</a><br/>
		<a id='myLink' href=''> Link to Rediff</a>

above code will render on web-page like below

Now to click the link of with linkText selector below statement needed

		driver.findElement(By.linkText("Link to Yahoo")).click();


As selector name it self suggests, it can select the Anchors(Links) by it’s partial text.

lets consider previous HTML snippet, if i want to select any link with partialLinkText selector, I need to give partial text of the link, check below snippet which will select the link


if I write below statement.


it will point to all links, as all links are having text “Link”, but as we are using .findWebElement  so it will point to first link in DOM, that is link to




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s