From c689166c48296bcb65a468eff4c30cd56dce60eb Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Thu, 17 Sep 2009 21:32:56 -0400 Subject: [PATCH] Fix "element.getAttribute is not a function" Selenium errors when filling in fields The root cause was the locator strategy was naively returning an element that was not a form field, causing Selenium's internals to blow up --- lib/webrat/selenium/location_strategy_javascript/label.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/webrat/selenium/location_strategy_javascript/label.js b/lib/webrat/selenium/location_strategy_javascript/label.js index 4352fef..dd13744 100644 --- a/lib/webrat/selenium/location_strategy_javascript/label.js +++ b/lib/webrat/selenium/location_strategy_javascript/label.js @@ -1,3 +1,4 @@ +// Credit to: http://simonwillison.net/2006/Jan/20/escape/ RegExp.escape = function(text) { if (!arguments.callee.sRE) { var specials = [ @@ -32,8 +33,10 @@ var locatedLabel = candidateLabels.first(); var labelFor = locatedLabel.getAttribute('for'); if ((labelFor == null) && (locatedLabel.hasChildNodes())) { - // TODO: should find the first form field, not just any node - return locatedLabel.firstChild; + return locatedLabel.getElementsByTagName('button')[0] + || locatedLabel.getElementsByTagName('input')[0] + || locatedLabel.getElementsByTagName('textarea')[0] + || locatedLabel.getElementsByTagName('select')[0]; } return selenium.browserbot.locationStrategies['id'].call(this, labelFor, inDocument, inWindow);