RegExp.escape = function(text) {
  if (!arguments.callee.sRE) {
    var specials = [
      '/', '.', '*', '+', '?', '|',
      '(', ')', '[', ']', '{', '}', '\\'
    ];
    arguments.callee.sRE = new RegExp(
      '(\\' + specials.join('|\\') + ')', 'g'
    );
  }
  return text.replace(arguments.callee.sRE, '\\$1');
}

var allLabels = inDocument.getElementsByTagName("label");
var regExp = new RegExp('^\\W*' + RegExp.escape(locator) + '(\\b|$)', 'i');

var candidateLabels = $A(allLabels).select(function(candidateLabel){
  var labelText = getText(candidateLabel).strip();
  return (labelText.search(regExp) >= 0);
});

if (candidateLabels.length == 0) {
  return null;      
}

//reverse length sort
candidateLabels = candidateLabels.sortBy(function(s) {
  return s.length * -1;
});

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 selenium.browserbot.locationStrategies['id'].call(this, labelFor, inDocument, inWindow);

@@ -1,20 +1,79 @@
+// Credit to: http://simonwillison.net/2006/Jan/20/escape/%0A
 RegExp.escape = func
@@ -1000,101 +1000,224 @@
 %7B%0A  
-// TODO: should find the first form field, not just any node%0A  return locatedLabel.firstChild
+return locatedLabel.getElementsByTagName('button')%5B0%5D%0A    %7C%7C locatedLabel.getElementsByTagName('input')%5B0%5D%0A    %7C%7C locatedLabel.getElementsByTagName('textarea')%5B0%5D%0A    %7C%7C locatedLabel.getElementsByTagName('select')%5B0%5D
 ;%0A%7D%0A

Fix "element.getAttribute is not a function" Selenium errors when filling in fields