GistLabs / mechanize

mechanize for java
http://gistlabs.com/software/mechanize-for-java/
Mozilla Public License 2.0
78 stars 21 forks source link

Get Form off of page returns null #76

Closed cmessinger closed 9 years ago

cmessinger commented 9 years ago

When creating a simple login script for a site such as google, when getting the form to set the values for the login credentials the form is null.

How is the look up done to get a form? I have tried the value of the id and action for the form both return null.

MechanizeAgent agent = new MechanizeAgent();
Page page = agent.get("http://www.google.com/");
Page resultPage = page.link("gb_70").click();
Form form = resultPage.form("https://accounts.google.com/ServiceLoginAuth");

-form is null

The page has the form that I need as can been seen using the code for (Form temp : resultPage.forms()) System.out.println(temp.getUri()); outputs: https://accounts.google.com/ServiceLoginAuth which is where it will post to.

jheintz commented 9 years ago

Hello Craig,

The page.form(String) method uses several mechanisms to lookup a form, but not the full URI.

The best option is to use the id or unique class attributes on the form, but that might not be stable for pages from google.

Also, the getUri() method is actually resolving between absolute and relative uri references with the base uri of the page.

If that URI value is the only stable thing you can use to find the form in question, then I think looping over the forms and looking for a matching value is your best option.

-John