How to find login form using RegsEx when using UIWebView to autofill data ?
I have used following code to get all form data:
int NoOfForms = [[self.browser stringByEvaluatingJavaScriptFromString:@"document.forms.length"] intValue];
NSMutableArray *formData = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0 ; i < NoOfForms ; i++)
{
// total number of elements in each form
int elementsInForm = [[self.browser stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.forms[%d].elements.length",i]] intValue];
for (int j = 0; j < elementsInForm; j++)
{
// element id of each element
NSString *elementID = [NSString stringWithFormat:@"document.forms[%d].elements[%d].id",i,j];
// element value of each element
NSString *elementValue = [NSString stringWithFormat:@"document.forms[%d].elements[%d].value",i,j];
// dictionary for all values of each element
NSDictionary *elementData = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:i],@"form No",
[NSNumber numberWithInt:j],@"element No",
[self.browser stringByEvaluatingJavaScriptFromString:elementID],@"elementID",
[self.browser stringByEvaluatingJavaScriptFromString:elementValue],@"elementValue",nil];
// add each element data to a mutable array.
[formData addObject:elementData];
}
}
And have the following code to put data back into their respective fields:
for (int i = 0 ; i < [dataArray count]; i++)
{
NSDictionary *elementDict = [dataArray objectAtIndex:i];
int formNumber = [[elementDict objectForKey:@"form No"] intValue];
int elementNumber = [[elementDict objectForKey:@"element No"] intValue];
NSString *formElementID = [elementDict objectForKey:@"elementID"];
NSString *formElementValue = [elementDict objectForKey:@"elementValue"];
if ([[self.browser stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.forms[%d].elements[%d].id",formNumber,elementNumber]] isEqualToString:formElementID])
{
[self.browser stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.forms[%d].elements[%d].value='%@'",formNumber,elementNumber,formElementValue]];
}
}
Now i want to submit only the login form..how to find it ?
I'm experiencing one more problem: when my code puts password into its field, some web sites like mail.google.com does let this happen i.e. it empties the password field. How to solve this problem ?
UPDATE : currently i have been able to log in on mail.google.com but could not get done the same for belleandclive.com . Is this site implementing any security measurements for its login form ?
I think you can judge which is the login form according to the form data or form id, right?
So, if the login form id is
login-form
, you can use the following code to submit it:or you can sumbit the form through judging which index is login form according the form data: