Logging into Ruby Mechanize does not work

Let me lay the foundation for what I'm trying to accomplish. In the physics class that I take, my teacher always likes to brag about how impossible it is to cheat in my class, because all her tasks are done through WebAssign. The way WebAssign works is this: everyone gets the same questions, but the numbers used in the question are random variables, so each student has different numbers, so a different answer. So I write ruby โ€‹โ€‹scripts to solve the issue for people just by imputing your specific numbers.

I would like to automate this process with the help of mechanization. I have used mechanization many times before, but I am having problems logging into the site. I will submit the form and it will return the same page that I just visited. You can look at the source code of the site, http://webassign.net , and I also tried using the login at http://webassign.net/login.html , no luck.

Let me do all this with some ruby โ€‹โ€‹code that doesn't do what I want:

require 'rubygems' require 'mechanize' agent = Mechanize.new page = agent.get("http://www.webassign.net/login.html") form = page.forms.last puts "Enter your username" form.WebAssignUsername = gets.chomp puts "Enter your password (Don't worry, we don't save this)" form.WebAssignPassword = gets.chomp form.WebAssignInstitution = "trinityvalley.tx" form.submit #=> Returns original page 

If someone is really interested in getting this to work, I would be more than happy to send them a working username and password.

+1
source share
4 answers

It can be verified on the site that the post Login variable is set (see the login button). Try adding form.Login = "Login" .

+3
source

Have you tried using agent.submit(form, form.buttons.first) instead of form.submit ?

This worked for me when I tried to file a form. At first I tried using form.submit and it saved the original page.

+3
source

Try setting up a user agent:

 agent = Mechanize.new do |a| a.user_agent_alias = 'Mac Safari' end 

Some sites seem to require this.

+1
source

Your question seems a bit ambiguous, saying you are out of luck? What is the problem? Do you get a different answer completely than when you view a page in a browser? If so, then do what @cam says and parse the headers, you can do it in Firefox through the extension, or you can do it in Chrome natively. In any case, try to imitate the headers you see in any browser you make in your user agent mechanism. Here is the script that I used to map iTunes request headers when I was processing data in the app store:

 def mimic_itunes( mech_agent ) mech_agent.pre_connect_hooks << lambda {|headers| headers[:request]['X-Apple-Store-Front'] = X_APPLE_STOREFRONT; headers[:request]['X-Apple-Tz'] = X_APPLE_TZ; headers[:request]['X-Apple-Validation'] = X_APPLE_VALIDATION; } mech_agent.user_agent = 'iTunes/9.1.1 (Windows; Microsoft Windows 7 x64 Business Edition (Build 7600)) AppleWebKit/531.22.7' mech_agent end 

Note: the constants in the example are just strings ... actually this is not so important if you know you can add any string there

Using this approach, you should be able to modify / add the headers that the web application may need.

If this is not the problem you are facing, send more details about what exactly is happening.

+1
source

Source: https://habr.com/ru/post/1340381/


All Articles