javascript - Mechanize 卡在 Javascript 重定向页面上

标签 javascript ruby-on-rails-3 redirect nokogiri mechanize

我在我的 Ruby on Rails 应用程序中使用 Mechanize 和 Nokogiri 来抓取我们的本地打印机管理面板,以检索打印机生命周期内的打印页数。

我有以下 rake 任务:

# Logs into printer admin page and retrieved counts.
require 'rubygems'
require 'mechanize'
require 'logger'

# Create a new mechanize object
agent = Mechanize.new

# Load the printer admin page
page = agent.get("http://192.168.1.126/index.html?lang=1")

# Select the form with an action of index.cqi
form = agent.page.form_with(:action => "index.cgi")
form.radiobuttons_with(:id => '0x3fdb24153404')[1]

# Submit the form
page = form.submit form.buttons.first

pp page

返回以下内容:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<script type="text/javascript">
<!--
window.onload=function(){setTimeout(function(){document.menu_link.submit();},0);}
//-->
</script>
</head>
<body>
<form name="menu_link" action="index.html" method="post" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="lang" value="1">
</form>
</body>
</html>

我似乎无法在上面的页面上选择表单,并且脚本似乎停在该页面上并且不遵循重定向。

是否有处理此类重定向的标准方法?也许暂停脚本直到重定向发生?它会允许重定向工作吗?

如有任何指点,我们将不胜感激!

最佳答案

您有两个选择。要么:

  1. Submit the form manually
  2. 使用WatirSelenium

基本上 Mechanise 不会运行 javascript,所以你必须手动模拟 javascript 运行(选项 1)或自动化一个真正的浏览器来执行它(选项 2)

如果您只是执行 lang=1POST 而不是 get,那么选项 1 应该是 double,因为这是所有表单正在执行的操作。

我猜是这样的:

page = agent.post('http://192.168.1.126/index.html', {
  "lang" => "1"
})

但我从未真正使用过 Mechanize。

关于javascript - Mechanize 卡在 Javascript 重定向页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10267289/

相关文章:

javascript - 如何检查单击时不起作用的按钮元素上的监听器?

ruby-on-rails - Rails 3 带有斜线和嵌套资源的路线

redirect - Haproxy ssl 重定向握手失败

Apache 代理通行证 : how to preserve original IP address

java - JMeter 中的网站 URL 正在重定向并且未加载测试 URL

javascript - 延迟从输入调度 React Redux Action

javascript - 使用函数式 javascript 和 ramda 查找和替换

javascript - PHP复制对象数组的一个字段到一个简单的数组

ruby-on-rails - 回形针错误 : Filename not recognized by the 'identify' command - ImageMagick

ruby-on-rails-3 - 单个模型上有多个 Carrierwave mount_uploader