2

I am trying to get hidden field with mechanize in ruby and trying to click on it.

agent = Mechanize.new
agent.get('http://www.example.com/')
agent.page.link_with(:text => "More Links...")

But this gives me:

=> nil

Actually, I want to click on it:

agent.page.link_with(:text => "More Links...").click

But this is an error:

undefined method `click' for nil:NilClass

And here is my HTML code:

<div id="rld-4" class="results_links_more highlight_d links_deep" style="display: none;">
<a class="large" href="javascript:;">More Links...</a>
</div>
pasawaya
  • 11,515
  • 7
  • 53
  • 92
sagar junnarkar
  • 1,240
  • 2
  • 10
  • 18

2 Answers2

1

Mechanize currently doesn't support javascript. I'd suggest you try and figure out what the server expects the user-agent to send and then replicate this with Mechanize. You can use a tool like HTTPFox which is a Firefox addon that monitors the traffic between a web server and your browser. Once you have this, you can easily replicate it with mechanize. Something like this;

agent = Mechanize.new

# Doesn't work

# home_page = agent.get('http://requestb.in/')
# agent.click(home_page.link_with(:text => "Create a RequestBin"))
# => undefined method `[]' for nil:NilClass (NoMethodError)

# Works

# The javascript code just makes a POST request with one parameter
request_bin = agent.post("http://requestb.in/api/v1/bins", { "private" => "false" })
puts request_bin.body
Kibet Yegon
  • 2,763
  • 2
  • 25
  • 32
0

That should probably find the link if it's really on the page, but the bigger problem is that clicking on a link with a href of 'javascript:;' doesn't do what you think it does. That's because mechanize is not a full browser with a javascript interpreter, etc.

pguardiario
  • 53,827
  • 19
  • 119
  • 159