ruby - 将 mechanize 与 watir + phantomjs 一起使用

标签 ruby macos phantomjs mechanize watir-webdriver

我正在尝试将从 phantom js 生成的 html 插入到 Mechanize 对象中,以便我可以轻松地搜索它。我试过以下无济于事......

b = Watir::Browser.new :phantomjs
url = "www.google.com"
b.goto url

agent = Mechanize.new
#Following is not executed at same time...
#Error 1: lots of errors
page = agent.get(b.html)

#Error 2: `parse': wrong number of arguments (1 for 3) (ArgumentError)
page = agent.parse(b.html)

#Error 3 last ditch effort: undefined method `agent' 
page = agent(b.html)

正如我所想的那样,我开始怀疑我是否可以 Mechanize 现有的 html 对象...我最初是通过以下方式了解它的:http://shane.in/2014/01/headless-web-scraping/ & http://watirmelon.com/2013/02/05/watir-webdriver-with-ghostdriver-on-osx-headless-browser-testing/

最佳答案

我也遇到过这种情况。我用 Mechanize 写了很多代码,所以我不想在使用 watir 时移动到 nokogiri。下面的代码是我的做法。

require 'watir'
require 'mechanize'

b = Watir::Browser.new
b.goto(url)
html = b.html
a = Mechanize.new
page = Mechanize::Page.new(nil, {'content-type'=>'text/html'}, html, nil, a)

您可以使用页面来搜索元素。

关于ruby - 将 mechanize 与 watir + phantomjs 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26879838/

相关文章:

python - 为什么负数在求平方时常常需要括号才能得到预期的结果?

ruby - RCov 不起作用

ruby-on-rails - send() 方法的用途是什么?

ruby - 如何将剪贴板内容粘贴到文件中?

macos - 使用 Homebrew 安装 dnsmasq

ruby-on-rails - 使用 capybara、phantomjs 和 Rails 在子域上测试 javascript

c - 有没有办法将控制台窗口永久附加到主 XCode 窗口?

cocoa - gdb 声称它不知道如何运行

javascript - 您将如何循环浏览每个图像并检查 casperjs 脚本中的错误?

javascript - 从 CasperJS 调用 Facebook API?