Search code examples
rubyauthenticationmechanizeaccount

Why mechanize failed to login website ruby


I'm trying to make an account to website "vpnstaticip.com" using mechanize but I've always got unknown error that

ERROR: Email is too long

after submiting.

The error never happend in the browser

I've tried to submit without any input and same thing happend

require "mechanize"

$url = "https://vpnstaticip.com/create-account.php?trial=1"

$m =  Mechanize.new
$page1 = $m.get($url)
$form1 = $page1.form_with(:id => "pro_form1")
$form1.field_with(:name => "name").value = "name"
$form1.field_with(:name => "email").value = "[email protected]"
$form1.field_with(:name => "country").options[217].click #United States
$form1.field_with(:name => "username").value = "Username"

$form1.checkbox_with(:name => "terms").check

$page2 = $m.submit($form1)

$file1 = open("vpnstaticip.html","w")
$file1.write($page2.parser)
$file1.close()

Solution

  • Looks like this form submitted by js, but the mechanize is not working with js actually

    onclick="document.getElementById('pro_form1').submit();"
    

    perhaps the changed of driver will help (selenium or poltergeist) with the creation of Capybara browser session.

    or, as an option, just try to ignore SSL errors:

    $m.verify_mode = OpenSSL::SSL::VERIFY_NONE
    

    or i think, match better to use here the net-post request without mechanize, for me it's work fine:

    url = URI("https://vpnstaticip.com/create-account.php?trial=1")
    
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    
    request = Net::HTTP::Post.new(url)
    request["content-type"] = 'application/x-www-form-urlencoded'
    request.body = "name=name&email=me%2540mail.com&country=217&username=Username&terms=1&nospam=nospam&submitted=1"
    
    response = http.request(request)
    puts response.read_body
    
    => ERROR: Username already exists. Try different one
    

    besides, i can't reproduce the "Email is too long" error, so please, let me know if the problem is still persist, thanks