Shorthand to XPath and CSS in developer console and javascript libraries but not WebDriver API?

15 Jul

This thought just came to mind while I blogged and commented online about finding elements by XPath and CSS using the DOM following DOM conventions (accessed/executed in javascript whether in browser developer console or with WebDriver JavascriptExecutor) like:




where some people pointed out you could just do

$x(‘//someXPath’) and $$(‘someCss’)

in the browser developer console (Firefox, Chrome) to do same thing. And yes, it is much shorter to type out.

And of course, you can do similar if your web application already uses jQuery or other javascript libraries, in which case you can execute their ways of locating elements with WebDriver’s JavascriptExecutor. If the web application under test doesn’t use them, the alternative if you want those location strategies is to inject the jQuery and like libraries into the page under test using JavascriptExecutor first before you can utilize them.

So it seems a bit interesting how some people prefer shorthand use in javascript library and in developer console, but how we don’t have much complaints or work done on the WebDriver side to mimic same changes on the web development/debugging side.

Like how document.getElementByXyz() is considered unnecessary extra work but driver.findElement(By.xpath()).click() is ok instead of targeting for something like driver.$x(‘//someXpath’).click() or driver.$$(‘some css’).click(). Granted in the WebDriver case, due to object oriented programming and instantiating classes, you’d always have a driver object that you can’t skip typing though you could shorten it by naming it “d”. But maybe we can also shorten the find by XPath and find by CSS methods with alias methods. And if there’s an argument of no, then one might also ask, since we can live with this with WebDriver, why complain that you can and should use shorthand in the browser console and jQuery?

I guess it is easier to code and debug when shorthand but standardization and consistency of APIs and naming to me is more important. Less to remember & learn, especially for novices. To me

document.getElementByXyz() clearly maps better to  driver.findElement( than $x() and $$(), and vice versa. Same applies for the multiple elements version of the methods.

2 Responses to “Shorthand to XPath and CSS in developer console and javascript libraries but not WebDriver API?”

  1. Anand Somani March 11, 2015 at 3:25 pm #

    Is getElementByXPath work?

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 )

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: