diff --git a/lib/capybara/driver/selenium_driver.rb b/lib/capybara/driver/selenium_driver.rb
index cc30dd6f..b24754b6 100644
--- a/lib/capybara/driver/selenium_driver.rb
+++ b/lib/capybara/driver/selenium_driver.rb
@@ -36,7 +36,7 @@ class Capybara::Driver::Selenium < Capybara::Driver::Base
end
def select(option)
- option_node = node.find_element(:xpath, ".//option[text()=#{Capybara::XPath.escape(option)}]") || node.find_element(:xpath, ".//option[contains(.,#{Capybara::XPath.escape(option)})]")
+ option_node = node.find_element(:xpath, ".//option[normalize-space(text())=#{Capybara::XPath.escape(option)}]") || node.find_element(:xpath, ".//option[contains(.,#{Capybara::XPath.escape(option)})]")
option_node.select
rescue
options = node.find_elements(:xpath, "//option").map { |o| "'#{o.text}'" }.join(', ')
@@ -49,7 +49,7 @@ class Capybara::Driver::Selenium < Capybara::Driver::Base
end
begin
- option_node = node.find_element(:xpath, ".//option[text()=#{Capybara::XPath.escape(option)}]") || node.find_element(:xpath, ".//option[contains(.,#{Capybara::XPath.escape(option)})]")
+ option_node = node.find_element(:xpath, ".//option[normalize-space(text())=#{Capybara::XPath.escape(option)}]") || node.find_element(:xpath, ".//option[contains(.,#{Capybara::XPath.escape(option)})]")
option_node.clear
rescue
options = node.find_elements(:xpath, "//option").map { |o| "'#{o.text}'" }.join(', ')
diff --git a/lib/capybara/spec/session/select_spec.rb b/lib/capybara/spec/session/select_spec.rb
index c17395be..b6348118 100644
--- a/lib/capybara/spec/session/select_spec.rb
+++ b/lib/capybara/spec/session/select_spec.rb
@@ -37,6 +37,12 @@ shared_examples_for "select" do
extract_results(@session)['locale'].should == 'jo'
end
+ it "match labels with preceding or trailing whitespace" do
+ @session.select("Lojban", :from => 'Locale')
+ @session.click_button('awesome')
+ extract_results(@session)['locale'].should == 'jbo'
+ end
+
context "with a locator that doesn't exist" do
it "should raise an error" do
running { @session.select('foo', :from => 'does not exist') }.should raise_error(Capybara::ElementNotFound)
diff --git a/lib/capybara/spec/views/form.erb b/lib/capybara/spec/views/form.erb
index 06cf50d5..ab3f90ed 100644
--- a/lib/capybara/spec/views/form.erb
+++ b/lib/capybara/spec/views/form.erb
@@ -77,6 +77,7 @@
+