我有这个简单的HTML:
<div> Test <span> someting </span></div>
Run Code Online (Sandbox Code Playgroud)
我怎样才能只检索innertextdiv的?
使用text检索div中的所有文本:
[1] pry(#<SandBox>)> first(:xpath, '//div').text
=> "Test someting"
Run Code Online (Sandbox Code Playgroud)
使用text()在我下面的错误XPath查询的结果:
[2] pry(#<SandBox>)> first(:xpath, '//div/text()')
Capybara::Poltergeist::BrowserError: There was an error inside the PhantomJS portion of Poltergeist. This is probably a bug, so please report it.
TypeError: 'null' is not an object (evaluating 'window.getComputedStyle(element).display')
Run Code Online (Sandbox Code Playgroud)
但是,使用与Nokogiri相同的XPath工作:
[3] pry(#<SandBox>)> Nokogiri::HTML(page.html).xpath('//div/text()').text
=> " Test "
Run Code Online (Sandbox Code Playgroud)
有没有办法只使用水豚而不诉诸Nokogiri?