在Mechanize中单击带有JavaScript的链接 [英] Clicking link with JavaScript in Mechanize

查看:84
本文介绍了在Mechanize中单击带有JavaScript的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:

<a class="top_level_active" href="javascript:Submit('menu_home')">Account Summary</a>

我想单击该链接,但是使用link_to时出现错误.

I want to click that link but I get an error when using link_to.

我尝试过:

bot.click(page.link_with(:href => /menu_home/))
bot.click(page.link_with(:class => 'top_level_active'))
bot.click(page.link_with(:href => /Account Summary/))

我得到的错误是: NoMethodError:nil:NilClass的未定义方法"[]"

The error I get is: NoMethodError: undefined method `[]' for nil:NilClass

推荐答案

这是一个javascript链接.机械化将无法单击它,因为它不评估javascript.抱歉!

That's a javascript link. Mechanize will not be able to click it, since it does not evaluate javascript. Sorry!

尝试找出当您单击该链接时浏览器中发生的情况.是否创建POST或GET请求?发送到服务器的参数是什么.知道这一点后,您就可以在Mechanize脚本中模拟相同的操作. Chrome开发人员工具/Firebug会帮忙.

Try to find out what happens in your browser when you click that link. Does it create a POST or GET request? What are the parameters that are sent to the server. Once you know that, you can emulate the same action in your Mechanize script. Chrome dev tools / Firebug will help out.

如果这不起作用,请尝试切换到支持javascript评估的库.我已经使用 watir-webdriver 取得了巨大的成功,但是您也可以尝试 casperjs pjscrape 或其他工具

If that doesn't work, try switching to a library that supports javascript evaluation. I've used watir-webdriver to great success, but you could also try out phantomjs, casperjs, pjscrape, or other tools

这篇关于在Mechanize中单击带有JavaScript的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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