Outline

In this post we will dive into the webdriver’s interrogation API. Interrogation simply described as 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

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

Prerequisites

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

Driver Level Interrogation

Driver level interrogation 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 driver of browsers. But if you check a text, this will not be a problem. All drivers of browsers returns the content of the page correctly.)

Driver Level Interrogation Example

DOM Level Interrogation

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 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.

Locating Elements with By Methods

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

Method Syntax Description
By.id driver.findElement(By.id(<element ID>)) Locates an element using the ID attribute
By.name driver.findElement(By.name(<element name>)) Locates an element using the Name attribute
By.LinkText driver.findElement(By.linkText(<linktext>)) Locates a link using link text
By.partialLinkText driver.findElement(By.partialLinkText(<linktext>)) Locates a link using the link’s partial text
By.className driver.findElement(By.className(<element class>)) Locates an element using the Class attribute
By.tagName driver.findElement(By.tagName(<htmltagname>)) Locates an element using the HTML tag

Locating 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:

Locating 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:

Locating 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.

Locating 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.

Locating 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 assertion.

interrogation

Locating 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 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 in 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 is shown in below example.

and the second way is using ByChained support class.

Locating Elements by CSS Selectors

First of all CSS selectors are faster J Yes it is faster and more flexible than XPath selectors. Thus it should be your default way to interrogate the web elements. I don’t want to dive into how to write CSS selectors but I can highly recommend you to check and bookmark below links for CSS selectors.

We can find an element’s CSS path very easily with Firefox Firepath addon. Its installation is described in Selenium-1 post.

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

  • Click FirePath tab
  • Select CSS: (X)
  • Click Arrow icon
  • Select First name Text Box
  • Related HTML will be highlighted.
  • Related CSS path will appear.

interrogation

This test’s code is shown below:

Locating 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 functions. You can find them in below link.

http://www.w3schools.com/xsl/xpath_intro.asp

http://www.w3.org/TR/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. Also you can read reference Xpath documents to write complex Xpath selectors.

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/[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]