Working with innerHTML or subset of HTML with Selenium

6 Nov

Not sure about Web Driver, but wonder why Selenium never had API method to get a subset of HTML, basically the innerHTML of a given element. Only offering to return the full HTML page source.

Perhaps because innerHTML is more of a developer used feature, probably not for testers who also happen to not have a web developer background?

But in any case, you can still get it via the DOM for Selenium RC.

html = selenium.getEval(“this.browserbot.findElement(\”id=someElementId\”).innerHTML”);

From a test execution performance standpoint, not sure if getting innerHTML is significantly slower than getting the page’s HTML source. But as an automator/developer, I’d rather not want to have to parse out the desired subset of HTML from the whole page source.

There is one benefit, for me anyways, to use innerHTML and that is to convert it to valid XML for further processing like traversal of the data via XML DOM via native code than via Selenium, which should be faster and more powerful to do in your native language than to do it via Selenium.

Update 2/9/2013

Thought I’d mention that for WebDriver, innerHTML DOM property is much easier to access. WebDriver treats it as an attribute of an element, so you can simply:

String elemHtml = driver.findElement(“someId”)).getAttribute(“innerHTML”);

You can then manipulate it as plain text, or convert & load as an XML DOM object.


2 Responses to “Working with innerHTML or subset of HTML with Selenium”

  1. December 5, 2012 at 9:29 pm #

    where do i have to put this html=?

    • David December 5, 2012 at 10:34 pm #

      In the example above, “html” is a variable you save the result to (actual syntax will differ based on programming language used, but the Selenium API call/code will remain roughly the same). You can then do whatever you want with the variable. As one might deduce, the data stored in the html variable is a string of HTML text (within/inside the element you specified). You can either manipulate it as text (via regular expressions or string search) or convert it into an XML DOM node object to iterate or traverse through (using your programming language’s XML DOM library of choice).

      As for how to use it, you replace “id=someElementId” in the example above with the actual locator you want to find and get innerHTML for. It can be id=, name=, xpath, css=, etc. You may want to parameterize that example call into a function where you can specify any locator to get innerHTML for. Do watch out for escaping double quotes, because we are executing Selenium API to call javascript code that requires quotes, so that’s why you have to escape them when used inside the javascript as in example above.

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

Tea in Eighteenth-Century Britain

History of Tea Project at Queen Mary University of London

Phimosis - A Simple Cure (That is working for me)

No nonsense, no adverts, no sales pitch, just honest information.

Glass Onion Blog

Cheat sheets, post-its and random notes from the desk of a programmer

Abode QA

A Hub For Testing Minds...

The Test Therapist

Performance & Security Testing Blog


a programmer's hub

Let's Not Crash and Burn

it makes your brain tingle


“Incinerate Ignorance”

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

a random walk through Computer Science research, 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

%d bloggers like this: