Tuesday, 4 September 2012
Wicked Good XPath: a faster JavaScript XPath library
Recent Posts
- New Google Tricks: 15+ Cool and Amazing Secret Hidden Google Tricks
- Weekend Tech News: Battlegrounds Mobile India gameplay, WhatsApp policy canceled?, OPPO foldable phone
- PUBG Mobile India release date: Relaunching as Battlegrounds Mobile India officially announced
- Difference B/W Blogs & Websites Things People Haven't Told You
- Watch Live IPL 2021: How to watch today's Live streaming of IPL 2021? All Methods
We are proud to release Wicked Good XPath, a Google-authored pure JavaScript implementation of the DOM Level 3 XPath specification. We believe it to be the fastest XPath implementation available in JavaScript.
To use Wicked Good XPath, simply download the wgxpath.install.js file and include it on your webpage with a script tag. For example:
<script src="wgxpath.install.js"></script>
Then call wgxpath.install() from your JavaScript code, which will ensure document.evaluate, the XPath evaluation function, is defined on the window object. To install the library on a different window, pass that window as an argument to the install function.
Despite the growing popularity of CSS selectors, XPath remains a useful mechanism to locate elements in an HTML document. It has particularly heavy usage in the context of frontend web testing tools like Selenium and Web Puppeteer. Sometimes there is simply no way to reference an element on the page other than with an XPath expression.
For those who have never used XPath, here is a taste:
On a Google search results page, the XPath expression //li[@class=āgā][3] identifies the third search result. Here is a snapshot from the Chrome extension XPath Viewer showing the third result selected when that expression is used.
A key challenge when using XPath, however, is that it is not natively supported on every browser. Most notably, Internet Explorer does not provide native XPath support for HTML documents. As a result, users turned towards pure JavaScript implementations of XPath. In 2005, Google engineers released AJAXSLT, which included a correct, but slow, XPath evaluator. Running web tests on IE using AJAXSLT was time-consuming.
Fast-forward to 2007, Cybozu Labs released JavaScript-XPath, a new JavaScript XPath implementation that was 10 times faster than AJAXSLT. The web testing tools began using it instead, and life was good... for a while. The JavaScript-XPath quickly fell out of maintenance, and bugs became tough to get fixed. Also, since it wasn't written in Google Closure, it was tricky for us Googlers to integrate into our JavaScript applications. A rewrite was necessary.
However, we went beyond merely porting the library to Google Closure and fixing a couple bugs. We identified some significant additional performance improvements, such that our version runs about 30% faster than the original. On top of that, the Closure compiler was able to minify our code down to a mere 25K, 40% smaller than JavaScript-XPath's 42K. Finally, the code is structured and documented in a way that we believe will make future maintenance quicker and easier.
We would like to thank our two Google interns, Michael Zhou and Evan Thomas, for doing the bulk of the development on this project.
By Greg Dennis and Joon Lee, Google Engineers
Label:
javascript,
open source release,
selenium,
web testing,
webdriver,
xpath
Related tricks
- London OS Jam 10: Off-Topic Is the New On-Topic
- Introducing Native Driver
- JQUERY: Automatic Timed & Self-Closing Facebook Popup Like Box For Blogger
- JQUERY: Simple Accordion Drop Down Menu With Jquery & CSS
- Is JavaScript and jQuery are actually the same thing
- Pave Your Way to World of Web Design with Knowledge of Programming Languages
- Wicked Good XPath: a faster JavaScript XPath library
- ReFr: A New Open-Source Framework for Building Reranking Models
- J2ObjC: A Java to iOS Objective-C translator
- Helping the World to Teach
- Leak Finder: a new tool for JavaScript
- Test your Web Apps on Chrome with ChromeDriver
- Test Your Mobile Web Apps with WebDriver - A Tutorial
- Introducing WebDriver
(+)