Web testing with Selenium and native XML/XPath

6 Nov

Not sure if Selenium RC (and also Selenium 2 / Web Driver) can find and manipulate or return a set of elements rather than a single one. I heard you can do that with Watir.

Anyhow, there’s one way alternate way to do this and is also a faster approach to working with the desired elements, particularly if you just need to read and process the values and not manipulate them.

Get the HTML page source, or better yet, innerHTML of the parent element for the elements you desire. If you get full page source, you may then need to parse out the subset of HTML you are interested in.

Then once you have that, convert to valid XML by append and prepend some root node tags, etc. to the HTML. This assumes that aside from the root node, the rest of the HTML can form valid XML, otherwise you would need further cleanup.

With the XML available, you can then pass it to XSLT processor (via code and an XSLT stylesheet) or by just traversing the XML as a DOM via XPath, returning desired nodeset to iterate over or to return specific single node or value to work with.

The XPath part is similar to using XPath for locators with Selenium. But now you can do much more with the elements in XML format to extract node names, text, and values.

The XML manipulation from native code of your language choice is likely a lot faster than having Selenium manipulate the set of elements.

One example where this is useful is parsing a table or data grid to verify content in the grid or do certain things with the content, say like fetch all values in a certain column in table which contains URL links and navigate to each of those links. This is especially useful when the grid/table is a lot of nested divs and tables via AJAX rather than a plain old basic non-nested HTML table.

One Response to “Web testing with Selenium and native XML/XPath”

  1. Ross Patterson January 27, 2012 at 2:46 pm #

    The Selenium RC API cannot return multiple elements in a single call. The WebDriver API can (see findElements()). But good post!

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Anastasia Writes

politics, engineering, parenting, relevant things over coffee.

One Software Tester

Trying To Make Sense Of The World, One Test At A Time

the morning paper

an interesting/influential/important paper from the world of CS every weekday morning, as selected by Adrian Colyer

RoboSim (Robot Simulator)

Visualize and Simulate the Robotics concepts such as Localization, Path Planning, P.I.D Controller


open notebook

a happy knockout mouse.

my journey into computer science

Perl 6 Advent Calendar

Something cool about Perl 6 every day


Inspire and spread the power of collaboration

Niraj Bhatt - Architect's Blog

Ruminations on .NET, Architecture & Design

Pete Zybrick

Bell Labs to Big Data

Seek Nuance

Python, technology, Seattle, careers, life, et cetera...


New Era of Test Automation

Der Flounder

Seldom updated, occasionally insightful.

The 4T - Trail, Tram, Trolley, Train

Exploring Portland with the 4T

Midnight Musings

Thoughts on making art

Automation Guide

The More You Learn The More You Play...!

The Performance Engineer



Thoughts related to software development

%d bloggers like this: