sagar junnarkar
sagar junnarkar

Reputation: 1270

Ruby:: How to search hidden elements with mechanize

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>

Upvotes: 2

Views: 732

Answers (2)

Kibet Yegon
Kibet Yegon

Reputation: 2823

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

Upvotes: 1

pguardiario
pguardiario

Reputation: 54984

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.

Upvotes: 0

Related Questions