gpt4 book ai didi

javascript - 自动填充后在 UIWebView 中执行 Javascript 后验证标签的长度

转载 作者:可可西里 更新时间:2023-11-01 06:15:27 26 4
gpt4 key购买 nike

为了明确这一点,我将拆分我的问题。

目标:

执行一些 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 语句,它总是会在 if 语句为真时将 if 语句验证为错误的登录凭据,这就是为什么我认为我在处理它之前先检查它。

我的猜测:

当 webView 尚未完成执行 javascript 和/或在单击按钮后重新加载网页时,我正在运行此 if 语句。

资源:

我不知道这有什么帮助,但我正在使用的网站是:tapgram.com/login

此处提供完整的当前代码:http://pastebin.com/dnCn62FP对于完整的项目代码,请发表评论,我会添加链接。

最佳答案

错误在 JavaScript 中。

NSString *failedLogin = [mainWebView 
stringByEvaluatingJavaScriptFromString:@"document.getElementById('login-msg')"];

返回 login-msg 元素的完整 HTML 字符串。

所以基本上你是在比较Failed login<h4 id=​"login-msg" style=​"color:​red;​">​Failed login​</h4> .

JavaScript 应该是这样的 document.getElementById('login-msg').innerText如果登录确实失败,它将返回 Failed login

关于javascript - 自动填充后在 UIWebView 中执行 Javascript 后验证标签的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20504389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com