I am trying to extract mixed mode content using Capybara. I did it using Nokogiri, but wonder why similar is not possible with Capybara.
require 'nokogiri'
doc = Nokogiri::HTML("<h1><em>Name</em>A Johnson </h1>")
puts doc.at_xpath("//h1/text()").content
It works, but when I try same XPath selector in Capybara it doesn't work.
visit('http://stackoverflow.com')
puts find(:xpath, "//h1/text()").text
It raises error:
[remote server] file:///tmp/webdriver-profile20120915-8089-kxrvho/extensions/fxdriver@googlecode.com/components/driver_component.js:6582:in `unknown': The given selector //h1/text() is either invalid or does not result in a WebElement. The following error occurred: (Selenium::WebDriver::Error::InvalidSelectorError)
[InvalidSelectorError] The result of the xpath expression "//h1/text()" is: [object Text]. It should be an element.
How to extract this text?
NameA Johnson
`, it will extract `NameA Jonson`, but I want just `A Jonson'. Using nokogiri in example above `doc.at_xpath("//h1/text()").content` works. Maybe I should first extract element with Capybara's find and after that pass it to nokogiri element, so I could write something like: `hr = find("/hr"); nokogiri::Element(hr).at_xpath("./text()").content` but I am not sure how. – Иван Бишевац Sep 15 '12 at 15:16