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(By.id(“someId”)).getAttribute(“innerHTML”);

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

Advertisements

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

  1. ivanka@abv.bg 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:

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

Seek Nuance

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

TELLURIUM

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

Code.Test.Tune.Optimize.

humblesoftwaredev

Thoughts related to software development

Yi Wang's Tech Notes

A blog ported from http://cxwangyi.blogspot.com

Appium Tutorial

Technical…..Practical…..Theoretically Interesting

LinuxMeerkat

I swear! Meerkats can do Linux

PacketsDropped

Requeuing the packets dropped in my memory.

Two cents of software value

Writing. Training. Consulting.

@akumar overflow

wisdom exceeding 140 chars.

Lazy Programmer's Shortcut

Java, J2EE, Spring, OOAD, DDD & LIFE! .......all in one :)

Testing Mobile Apps

www.SoftwareTestingStudio.com

Photofocus

education and inspiration for visual storytellers

%d bloggers like this: