In this post, we will dive into the Selenium Webdriver Interrogation API. In order words, Selenium Find Element API. Finding elements simply described to get information from the page. Webdriver has two levels of interrogation, these are driver and DOM level interrogations. I will explain and show examples of both levels of interrogations.

Audience

Selenium Webdriver Interrogation post is designed for beginners who have little or no automation experience. At the end of this tutorial, you will have knowledge of Selenium Webdriver Interrogation functions.

Prerequisites

Nice to have: Before starting to read this post it is better to read previous selenium tutorials.

  1. http://www.swtestacademy.com/selenium-tutorial-java-junit/
  2. http://www.swtestacademy.com/selenium-webdriver-api/
  3. http://www.swtestacademy.com/selenium-webdriver-navigation/

Driver Level Selenium Find Element 

Driver level interrogation – selenium find element – consists of below functions:

. getTitle() – returns page title as String

.getCurrentUrl() – returns current URL as String

.getPageSource() – returns page source as String (Note: Webdriver returns some page source differences depends on a driver of browsers. But if you check a text, this will not be a problem. All drivers of browsers return the content of the page correctly.)

Driver Level Selenium Webdriver Interrogation Example

DOM Level Selenium Find Element 

First of all, we have to start with what is DOM. DOM expansion is Document Object Model. The Document Object Model is a programming API for HTML and XML documents. It defines the logical structure of HTML and the way a page is accessed and manipulated.

DOM Example:

interrogation_1

* For more information you can visit http://www.w3.org/TR/WD-DOM/introduction.html

Everything in a web page such as text box, button, paragraph, etc. is a web element in webdriver. We can interrogate web elements via webdriver’s interrogation methods such as .getText(), isSelected(), .getSize(), .getLocation(), etc. Interrogation can be done in two steps. First, we have to find the element which we want to interrogate and then we can interrogate it with webdriver’s interrogation methods. For example, if we want to get the text of Google’s “Gmail” link:

interrogation_2

1) We have to find the element via id, CSS, or XPath selectors. In this example I used Xpath.
(Note: Selectors will be described later posts so now just see their usage. How to find an elements XPath via firebug is explained Selenium-1 post.)

2) Get Gmail link’s text via .getText() method.

Finding Elements with By Methods

Webdriver’s find methods with “By” is listed below.

MethodSyntaxDescription
By.iddriver.findElement(By.id(<element ID>))Locates an element using the ID attribute
By.namedriver.findElement(By.name(<element name>))Locates an element using the Name attribute
By.LinkTextdriver.findElement(By.linkText(<linktext>))Locates a link using link text
By.partialLinkTextdriver.findElement(By.partialLinkText(<linktext>))Locates a link using the link’s partial text
By.classNamedriver.findElement(By.className(<element class>))Locates an element using the Class attribute
By.tagNamedriver.findElement(By.tagName(<htmltagname>))Locates an element using the HTML tag

Selenium Find By Id

www.linkedin.com is our test site

1) Navigate to linkedin.com

2) Use Firebug to select first name text box to reach its html

interrogation

3) In above picture, you can see that in this case ID=first-name

Test code is shown below:

Selenium Find By Name

1) Use Firebug to select first name text box to reach its HTML

interrogation

2) In above picture, you can see that in this case name=firstName

Test code is shown below:

Selenium Find By LinkText

This locator is useful for only hyperlink texts. We get the link via link’s text.

1) Use Firebug to select “Forgot password?” link and highlight its HTML.

interrogation

2) In above picture, you can see that in this case link’s text is “Forgot Password?” We can reach this link by using By.linkText method.

Selenium Find By PartialLinkText

With this method, we can locate the link with its partial text.

1) Use Firebug to select “Forgot password?” link and highlight its HTML.

interrogation_5

2) In above picture, you can see that in this case link’s text is “Forgot Password?” We can reach this link by using By.partiallinkText method.

Selenium Find By ClassName

We can access any object with its class name. In below example, we will count how many web element is used “cell-body-textinput” class. Our test site is again linkedin.com and we saw that there are four web elements is using “cell-body-textinput” class. Below test, we get and count how many web elements are using “cell-body-textinput” class and check the result with an assertion.

interrogation

Selenium Find By TagName

We can get the web elements by using their tag names. For example in linkedin.com there are 4 <h3> tags available. In below example, we will count this <h3> tags and check its count via assert function.

interrogation

Note: If we cannot locate the web element successfully, then we will get “noSuchElementException”. We can write our test with try/catch blocks as shown below.

Chaining with findElements Methods and ByChained Support Class

In any web page, there are more than one web elements have got the same id, name, class, etc. We can interrogate any web element by chaining its id, name, tag name etc. and we can do that in two ways:

  • Chaining with findElement by methods
  • Chaining with ByChained support class

You can find this two ways in below examples.

Again our test site is linkedin.com main page 🙂

As you can see below picture there are several links on the main page under “div class=link” and our aim to locate “About” link.

interrogation_9

To locate “About” link, first, we will chain findElement methods which are shown in below example.

and the second way is using ByChained support class.

Finding Elements by CSS Selectors

First of all CSS selectors are faster. Yes, it is faster and more flexible than XPath selectors. Thus, it should be your default way to interrogate the web elements.

You can learn how to write effective CSS locators in below article.

http://www.swtestacademy.com/css-locators/

We can find an element’s CSS path very easily with Ranorex Selocity addon. Its installation is described in RanorexSelocity post.

For example, if we want to get first name text box’s CSS path,

    • Click FirePath tab (FirePath deprecated! Please use Ranorex Selocity.)
    • Select CSS: (X)
    • Click Arrow icon

Select First name TextBox

  • Related HTML will be highlighted.
  • Related CSS path will appear.

interrogation

This test’s code is shown below:

Finding Elements by XPath Selectors

XPath is generally slower than CSS. It is better to use CSS rather than XPath but if you really get in trouble by interrogation with CSS then that time you can use XPath. I do not want to go into detail of all XPath here.

You can find how to write smart and affective XPath locators in below article.

http://www.swtestacademy.com/smart-xpath/

In our codes, we use “By.xpath” syntax to interrogate by XPath selectors. I below example, I used firepath plugin to get XPath selector. FirePath deprecated! Please use Ranorex Selocity.

interrogation

This is the test code:

CSS & XPath Reference:

In below reference all the properties and functions of XPath, CSS, DOM are written side by side. I highly recommend this reference and also you can download this reference as PDF from below link.

https://www.simple-talk.com/dotnet/.net-framework/xpath,-css,-dom-and-selenium-the-rosetta-stone/

Selenium Webdriver Tutorial Series