1

我一直在环顾四周,发现了很多用于自动填充的代码,如下所示:

 - (void)webViewDidFinishLoad:(UIWebView *)webView
{
//For Authentication
NSString *savedUsername = @"Bapu";
NSString *savedPassword = @"Bapu123";

if (savedUsername.length != 0 && savedPassword.length != 0)
{
    //create js strings
    NSString *loadUsernameJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='text']\"); \
                                for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedUsername];
    NSString *loadPasswordJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='password']\"); \
                                for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedPassword];

    //autofill the form
    [webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
    [webView stringByEvaluatingJavaScriptFromString: loadPasswordJS];
}
}

这就是我的 .m 文件中的内容,但是,当我转到 webView 时,这些字段仍然是空白的。关于为什么它不能填充的任何线索?

如果有帮助,我要填写的网站是:sis.ocsarts.net,相关代码是:

input type="text" size="20" name="UserName" value=""  onkeydown="if (event.keyCode==13) { document.frmLogin.submit(); return false; };"

input type="password" size="20" name="Password"  onkeydown="if (event.keyCode==13) { document.frmLogin.submit(); return false; };"

我的 FirstViewController.m 的完整代码是:

//
//  FirstViewController.m
//  OCSA
//
//  Created by Jack Kearl on 4/1/13.
//  Copyright (c) 2013 Jack Kearl. All rights reserved.
//

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

@synthesize Aeries;

- (void)viewDidLoad
  {
  [super viewDidLoad];
  [Aeries loadRequest:[NSURLRequest requestWithURL:
  [NSURL URLWithString:@"http://sis.ocsarts.net/login.asp"]]];

  }

 - (void)webViewDidFinishLoad:(UIWebView *)webView
 {
//For Authentication
NSString *savedUsername = @"Bapu";
NSString *savedPassword = @"Bapu123";

if (savedUsername.length != 0 && savedPassword.length != 0)
{
    //create js strings
    NSString *loadUsernameJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='text']\"); \
                                for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedUsername];
    NSString *loadPasswordJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='password']\"); \
                                for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedPassword];

    //autofill the form
    [webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
    [webView stringByEvaluatingJavaScriptFromString: loadPasswordJS];
}
}


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
4

1 回答 1

0

您尝试访问的网站http://sis.ocsarts.net包含指向http://sis.ocsarts.net/login.asp的框架。

将 Web 视图更改为从该 login.asp 页面加载。

我通过使用 Chrome 开发者控制台发现了它,试图在你提到的原始 URL 上执行 javascript,只是为了发现获取输入元素的 javascript 函数总是返回空数组。我什至尝试了 getElementsByName、getElementsByTagName 等。

确保 webview 委托已链接并尝试将 javascript 字符串合并为一个:

NSString *loadUsernameAndPasswordJS = [NSString stringWithFormat:@"document.getElementsByTagName('input')[1].value = '%@';document.getElementsByTagName('input')[2].value = '%@';", savedUsername, savedPassword];
于 2013-05-16T16:36:26.310 回答