在水豚中选择混合模式内容 [英] Select mixed mode content in Capybara

查看:56
本文介绍了在水豚中选择混合模式内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Capybara提取混合模式内容。我是使用Nokogiri进行的,但想知道为什么水豚无法做到这一点。

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

它可以工作,但是当我在Capybara中尝试相同的XPath选择器时,它不起作用。

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

它引发错误:

[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.

如何提取此文本?

推荐答案

Capybara需要驱动程序,并且XPath将由驱动程序执行。从错误消息中可以很明显地看出您正在使用selenium-webdriver,它将使用浏览器的本机XPath实现(如果有)。对于IE,它使用自己的。

Capybara requires a driver, and the XPath will be executed by the driver. From your error message, it is clear you are using selenium-webdriver, which will use a browser's native XPath implementation where available. For IE, it usees its own.

您似乎正在使用XPath实现不完全兼容的组合。您可以尝试更改驱动程序或浏览器,但是如果您真的想使用Nokogiri提取内容,则应该可以执行以下操作:

You appear to be using a combination where the XPath implementation is not fully compliant. You can try to change the driver or browser, but if you really want to use Nokogiri to extract content, you should be able to do the following:

doc = Nokogiri::HTML(page.html)
puts doc.at_xpath("//h1/text()").content

这篇关于在水豚中选择混合模式内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆