Selenium-4: Webdriver Interrogation


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. 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 locatos in below article.

How to Write Effective CSS Locators

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 here. You can find how to write smart and affective XPath locators in below article.

How to Write Smart XPath Locators

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

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/

javafx

Database Operations in JavaFX

By Onur Baskirt / Apr 1, 2016 / 56 Comments
Before started this section, please check the first article and learn How to Start JAVAFX! http://www.swtestacademy.com/getting-started-with-javafx/ At first, part of JavaFX tutorial series, we created a sample JavaFX project, designed the draft version of the UI and set up an...
rest assured

REST API Testing with Rest Assured

By Onur Baskirt / Mar 8, 2016 / 32 Comments
Outline In this post, I will explain what is API and API testing, what is the difference between SOAP and REST services, and how to test REST APIs with Rest Assured Library. What is API? API stands for Application Programming...
extentreports

How to Write Effective CSS Locators

By Onur Baskirt / Oct 1, 2017 / 0 Comments
Hi all, in this tutorial, I will describe you how to write effective CSS locators to interrogate web elements for your automation projects. As a rule of thumb, your interrogation strategy should be in below order: First try to use...
extentreports

Selenium-11: Execute JavaScript with JavascriptExecutor

By Onur Baskirt / Jan 27, 2016 / 15 Comments
Outline Sometimes we cannot handle some conditions or problems with Webdriver, web controls don't react well against selenium commands. In this kind of situations, we use Javascript. It is useful for custom synchronizations, hide or show the web elements, change...
javafx

Getting Started with JavaFX

By Onur Baskirt / Mar 25, 2016 / 0 Comments
When I started to work in my current position, one of my task is to do manual operations for campaign products  every week. After the second week, I thought that I have to automate this task using a GUI based...
extentreports

How to Write Smart XPath Locators

By Onur Baskirt / Sep 24, 2017 / 4 Comments
Hi all, in this tutorial, I will describe you how to write smart and non-brittle XPath locators. When we write our test scripts, we generally prefer to use id, name, class, etc. these kinds of locators. However, sometimes we could not...
extentreports

Selenium-1: Quick Start to Automation with Selenium WebDriver & JAVA & JUnit & Maven & IntelliJ

By Onur Baskirt / Sep 8, 2015 / 26 Comments
Outline Selenium Webdriver is the most popular open source web test automation framework across wide range of browsers and platforms. In this tutorial you will learn how to do web test automation with Selenium Webdriver and the related tools. Audience...
page object model

Page Object Model with C#

By Ege Aksoz / Jun 18, 2017 / 8 Comments
In the previous tutorial, we’ve taken the initial steps and entered the world of automated testing. We also wrote our first automated test. From this point on, since we are not just going to write one test, we need to...
extentreports

How to Select a Date From DatePicker Using Selenium

By Onur Baskirt / Aug 13, 2016 / 6 Comments
When you need to automate a airway, hotel, or similar websites you need to deal with Datepickers and some times it is a little bit cumbersome to select a specific date on the Datepicker or calendar.  In this post, I...
extentreports

Selenium Webdriver Performance testing with Jmeter and Selenium Grid

By Ozgur Kaya / Aug 9, 2016 / 2 Comments
In this post, we will complete Selenium Webdriver Performance testing scenario using Jmeter and Selenium Grid. 1- Install Java 7 or later If necessary https://java.com/tr/download/ 2- Download latest Jmeter version 3.0 or higher. http://jmeter.apache.org/download_jmeter.cgi 3- Download Jmeter PluginsManager JAR file and...
By | 2017-10-01T01:42:51+00:00 December 12th, 2015|Selenium, Test Automation, Web Automation|4 Comments

About the Author:

Onur Baskirt
Onur Baskirt is a senior IT professional with 10+ years of experience. He worked at Bahçesehir University, ST Microelectronics, Huawei and Ericsson as research assistant, design verification engineer and software test leader. Also, he worked as software test leader and software operations manager at Turkey's biggest technology retailer, Teknosa. Now, he is working as Head of Software Testing and Manager of two Development Teams at Kariyer.net. His current research areas are technical software testing, programming, and computer science. Formerly, he has several research works and papers on digital chip design & verification. His hobbies are sport, dancing, traveling, and nutrition. You can find detailed information about him on his linked-in page.

4 Comments

  1. Alexey November 18, 2016 at 7:47 am - Reply

    many thanks for your extremely cool and clear articles!

    • Onur Baskirt
      Onur Baskirt November 19, 2016 at 8:59 pm - Reply

      You are welcome Alexey. I will continue to write new ones. Please, stay tuned 😉

  2. Kumar Gaurav November 27, 2016 at 9:49 am - Reply

    Well Draftet, Onur!

    Love to see all articles related to Selenium Webdrivers. All of them were written with full of maturity & making sure that audience will improve there technical level.
    Love to see more articles of yours,

    Regards,
    Kumar Gaurav | India

    • Onur Baskirt
      Onur Baskirt November 27, 2016 at 1:00 pm - Reply

      Thank you Kumar. I will try to put more content to the site.

Leave A Comment