To clarify the issue I posted earlier: I am on OS X 10.6.7. Trying to use Mechanize to log in to LinkedIn. As others have posted about in the past, when I submit the form it kicks me back to the LinkedIn landing page, and does not log me in. I read the earlier discussion of the issue that mentioned how cookie values were being improperly dequoted when stored. But I thought that issue was fixed. I tried manually entering quotes back in to the cookie values, but so far that hasn''t worked for me. I am able to log in just fine using Watir, so I have to assume that this is some kind of javascript or cookie issue. I would really rather use mechanize here because in the particular app I am working on, Watir would be something of a burden. Here is the code I was using (minus adding quotes to cookies), which I modified from the discussion I found here: http://bit.ly/e6J38E require ''rubygems'' if VERSION < ''1.9'' require ''open-uri'' require ''nokogiri'' require ''mechanize'' agent = Mechanize.new agent.read_timeout = 300 agent.open_timeout = 60 agent.max_history = 1 # Prevent excessive memory usage agent.user_agent_alias = ''Mac Safari'' page = agent.get(https://www.linkedin.com/secure/login?trk=hb_signin) form = page.form(''login'') form.session_key = USERNAME form.session_password = PASSWORD new_page = form.click_button Any help would be appreciated. Lonny Eachus ===========-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/mechanize-users/attachments/20110327/f3233d73/attachment.html>
Lonny, Assuming that you''re not getting hit by the captcha, the reason is that Linkedin uses JavaScript to redirect instead of a standard HTTP Location header or a meta tag. Mechanize does not interpret JavaScript, so it cannot follow the redirect. However, you can still login by either manually interpreting and following the JavaScript redirect, or following the ''click here'' to redirect link. IWantToUseJavaScript = true require ''mechanize'' agent = Mechanize.new agent.set_proxy ''localhost'', 8080 page = agent.get ''http://www.linkedin.com/'' page = page.link_with(:text => /Sign in/).click form = page.form_with(:name => ''login'') form.session_key = ''benmanns at gmail.com'' form.session_password = ''mypassword'' page = form.submit # form.button_with(:value => /Sign In/) if IWantToUseJavaScript page = agent.get page.search(''script[type="text/javascript"]'').select { |script| script.content.match(/window\.location\.replace\(''([^'']+)''\);/) }.first.match(/window\.location\.replace\(''([^'']+)''\);/)[1] else page = page.link_with(:text => /click here/).click end I suggest using the latter, as parsing JavaScript with regular expressions can be a bit messy. If you do decide to follow the JavaScript method, my code needs to be cleaned up, and doesn''t handle when a URI contains apostrophes (''). Good Luck, Ben On Sun, Mar 27, 2011 at 6:00 PM, Lonny Eachus <lonny6 at gmail.com> wrote:> > To clarify the issue I posted earlier: > > I am on OS X 10.6.7. Trying to use Mechanize to log in to LinkedIn. As > others have posted about in the past, when I submit the form it kicks me > back to the LinkedIn landing page, and does not log me in. > > I read the earlier discussion of the issue that mentioned how cookie values > were being improperly dequoted when stored. But I thought that issue was > fixed. I tried manually entering quotes back in to the cookie values, but so > far that hasn''t worked for me. > > I am able to log in just fine using Watir, so I have to assume that this is > some kind of javascript or cookie issue. I would really rather use mechanize > here because in the particular app I am working on, Watir would be something > of a burden. > > Here is the code I was using (minus adding quotes to cookies), which I > modified from the discussion I found here: http://bit.ly/e6J38E > > > require ''rubygems'' if VERSION < ''1.9'' > require ''open-uri'' > require ''nokogiri'' > require ''mechanize'' > > agent = Mechanize.new > agent.read_timeout = 300 > agent.open_timeout = 60 > agent.max_history = 1 # Prevent excessive memory usage > agent.user_agent_alias = ''Mac Safari'' > > page = agent.get(https://www.linkedin.com/secure/login?trk=hb_signin) > > form = page.form(''login'') > form.session_key = USERNAME > form.session_password = PASSWORD > new_page = form.click_button > > > Any help would be appreciated. > > Lonny Eachus > ===========> > _______________________________________________ > Mechanize-users mailing list > Mechanize-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/mechanize-users >-- Benjamin Manns benmanns at gmail.com (434) 321-8324 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/mechanize-users/attachments/20110329/65aa1287/attachment-0001.html>
No, the problem is as I stated. It does indeed redirect, but to the wrong place: I end up on a redirect page, but the link is back to the home (landing) page, not the user page it should go to. It appears that Mechanize is NOT successfully logging in, for whatever reason, precisely as described in the link I first gave. At least that is the appearance of things... it might be failing for a different reason but it is still failing, and trying to redirect me to the landing page ("http://www.linkedin.com/home") rather than the user''s home page ("http://www.linkedin.com/nhome"). If anybody has an answer, I would really appreciate it. It it important for me to do this, and I have been pulling my hair. Lonny Eachus ===========> ------------------------------ > > Message: 7 > Date: Tue, 29 Mar 2011 09:53:28 -0400 > From: Benjamin Manns <benmanns at gmail.com> > To: Ruby Mechanize Users List <mechanize-users at rubyforge.org> > Subject: Re: [Mechanize-users] LinkedIn still not working? > Message-ID: > <AANLkTikg4qKN0Gttf=OkvuR8wJXtx-Td5fN49EnEuUPy at mail.gmail.com> > Content-Type: text/plain; charset="iso-8859-1" > > Lonny, > > Assuming that you''re not getting hit by the captcha, the reason is that > Linkedin uses JavaScript to redirect instead of a standard HTTP Location > header or a meta tag. Mechanize does not interpret JavaScript, so it cannot > follow the redirect. However, you can still login by either manually > interpreting and following the JavaScript redirect, or following the ''click > here'' to redirect link. > > IWantToUseJavaScript = true > require ''mechanize'' > agent = Mechanize.new > agent.set_proxy ''localhost'', 8080 > page = agent.get ''http://www.linkedin.com/'' > page = page.link_with(:text => /Sign in/).click > form = page.form_with(:name => ''login'') > form.session_key = ''benmanns at gmail.com'' > form.session_password = ''mypassword'' > page = form.submit # form.button_with(:value => /Sign In/) > if IWantToUseJavaScript > page = agent.get page.search(''script[type="text/javascript"]'').select { > |script| script.content.match(/window\.location\.replace\(''([^'']+)''\);/) > }.first.match(/window\.location\.replace\(''([^'']+)''\);/)[1] > else > page = page.link_with(:text => /click here/).click > end > > I suggest using the latter, as parsing JavaScript with regular expressions > can be a bit messy. If you do decide to follow the JavaScript method, my > code needs to be cleaned up, and doesn''t handle when a URI contains > apostrophes (''). > > Good Luck, > > Ben >