Clicking link with JavaScript in Mechanize

前端 未结 3 1121
闹比i
闹比i 2021-01-03 03:47

I have this:

Account Summary

I want to c

相关标签:
3条回答
  • 2021-01-03 04:24

    Here's a way to handle it. Assume your page returns this content:

    puts page.body
    <HTML><SCRIPT LANGUAGE="JavaScript"><!--
         top.location="http://www.example.com/pages/myaccount/dashboard.aspx?";
    // --></SCRIPT>
    <NOSCRIPT>Javascript required.</NOSCRIPT></HTML>
    

    We know it's coming so we know what to check for:

    link_search = %r{top.location="([^"]+)"}
    js_link = page.body.match(link_search)[1]
    page = agent.get(js_link)
    
    0 讨论(0)
  • 2021-01-03 04:34

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

    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.

    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

    0 讨论(0)
  • 2021-01-03 04:42

    The first 2 should have worked so try this, print out the hrefs to make sure it's really there:

    puts page.links.map(&:href)
    

    Remember that just because you can see it in your browser does not mean it appears in the response. It could have been sent as an ajax update. Also you can just do this which I think is cleaner:

    page.link_with(:href => /menu_home/).click
    

    However I don't think clicking that link will do what you want since it's javascript.

    0 讨论(0)
提交回复
热议问题