我正在尝试使用 Capybara 提取混合模式内容。我是用 Nokogiri 做的,但想知道为什么 Capybara 不可能做到类似。
require 'nokogiri'
doc = Nokogiri::HTML("<h1><em>Name</em>A Johnson </h1>")
puts doc.at_xpath("//h1/text()").content
它可以工作,但是当我在 Capybara 中尝试相同的 XPath 选择器时它不起作用。
visit('http://stackoverflow.com')
puts find(:xpath, "//h1/text()").text
它引发错误:
[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.
如何提取此文本?