在自动填充后,在UIWebView中执行Javascript后验证Label的长度
Verify the length of a Label after executing Javascript in UIWebView after auto-fill
为了说明这一点,我将把我的问题分开。
目标:
执行一些Javascript后,检查页面上的a元素ONCE;但如果不正确,我希望按下按钮就能重新检查。
我尝试过的:
-(IBAction)signin {
[passwordTF resignFirstResponder];
[usernameTF resignFirstResponder];
//create js strings
NSString *loadUsernameJS = [NSString stringWithFormat:@"var field = document.getElementById('login-form-username'); field.value='%@';", username];
NSString *loadPasswordJS = [NSString stringWithFormat:@"var field = document.getElementById('login-form-password'); field.value='%@';", password];
//autofill the form
[mainWebView stringByEvaluatingJavaScriptFromString:loadUsernameJS];
[mainWebView stringByEvaluatingJavaScriptFromString:loadPasswordJS];
//auto login
[mainWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('submit-login').click();"];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"webViewDidiFinishLoad called");
if (shouldVerify == YES) {
NSLog(@"webViewDidiFinishLoad - shouldVerify YES");
NSString *failedLogin = [mainWebView stringByEvaluatingJavaScriptFromString: @"document.getElementById('login-msg')"];
if ([failedLogin isEqualToString:@"Failed login"]) {
NSLog(@"webViewDidiFinishLoad - shouldVerify YES - failed");
UIAlertView *WCE = [[UIAlertView alloc] initWithTitle:@"Wrong Credentials" message:@"The username or password you entered is incorrect please try again." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[WCE show];
[keychainItem resetKeychainItem];
password = [keychainItem objectForKey:(__bridge id)(kSecValueData)];
username = [keychainItem objectForKey:(__bridge id)(kSecAttrAccount)];
[usernameTF setText:username];
[passwordTF setText:password];
} else {
NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");
ViewController *viewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];
UINavigationController *navBar=[[UINavigationController alloc]initWithRootViewController:viewController];
navBar.navigationBarHidden = YES;
[self presentViewController:navBar animated:YES completion:nil];
}
shouldVerify = NO;
}
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeOther) {
NSLog(@"shouldStartLoadWithRequest - shouldVerify YES");
shouldVerify = YES;
} else {
NSLog(@"shouldStartLoadWithRequest - shouldVerify NO");
shouldVerify = NO;
}
return YES;
}
问题:
出于某种原因:
if ([failedLogin isEqualToString:@"Failed login"]) {
NSLog(@"webViewDidiFinishLoad - shouldVerify YES - failed");
UIAlertView *WCE = [[UIAlertView alloc] initWithTitle:@"Wrong Credentials" message:@"The username or password you entered is incorrect please try again." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[WCE show];
} else {
NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");
}
if语句总是返回false和
NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");
在不应该打印的时候被打印出来,failedLogin等于@"Failed Login"。如果我删除else语句,它总是在为true时将If语句验证为虚假登录凭据,这就是为什么我认为在处理它之前,我应该先检查它。
我的猜测:
当webView还没有执行完javascript和/或在点击按钮后重新加载网页时,我正在运行这个if语句。
资源:
我不知道这有什么帮助,但我正在使用的网站是:tapgram.com/login
完整的当前代码可在此处获得:http://pastebin.com/dnCn62FP完整的项目代码请评论,我会添加链接。
错误在JavaScript中。
NSString *failedLogin = [mainWebView
stringByEvaluatingJavaScriptFromString:@"document.getElementById('login-msg')"];
返回登录消息元素的完整HTML字符串。
所以基本上你是在比较Failed login
和<h4 id="login-msg" style="color:red;">Failed login</h4>
。
JavaScript应该是这样的document.getElementById('login-msg').innerText
,如果登录真的失败,它将返回失败登录。
相关文章:
- 正在验证8个真/假复选框或复选框中的2个
- 借助asp.net验证或java脚本对多个文本进行验证
- jQuery自定义验证比较多个输入的序列
- 使用html中的外部javascript进行数据验证
- 如何使用jquery Validation验证Formspread
- jquery中的文本框验证
- 在验证和发送邮件后更改联系人表单的 html
- 代码不会验证
- JS验证ajax返回的html中的表单数据
- 同步调用,直到用户通过angular验证为访问者
- 带有加号的电话号码验证(可选)
- 解析javascript表单验证器
- 两位数的月份日期验证
- 使用angularjs验证文本框中的电子邮件
- 验证Javascript中的Textarea
- 使用regex的jquery keydown绑定不会验证撇号和句点
- Jquery表单验证插件-如果选中复选框,如何在提交时执行某些操作
- 正在删除node.js中已验证的网站
- 自定义表单验证和提交
- 在自动填充后,在UIWebView中执行Javascript后验证Label的长度