在自动填充后,在UIWebView中执行Javascript后验证Label的长度

Verify the length of a Label after executing Javascript in UIWebView after auto-fill

本文关键字:Label 验证 Javascript 填充 UIWebView 执行      更新时间:2024-07-01

为了说明这一点,我将把我的问题分开。

目标:

执行一些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,如果登录真的失败,它将返回失败登录