Pre-fill WebView text fields

大憨熊 提交于 2019-12-05 08:59:59

I had obtained the element IDs by highlighting them in Firefox and selecting inspect element. Turns out this gives different IDs to those returned with:

NSString *body = [self.emailWebView stringByEvaluatingJavaScriptFromString:
                  @"document.getElementsByTagName('body')[0].outerHTML"];            
NSLog(@"%@", body);

Plugging these IDs (which apparently vary dependent upon what was used to navigate to the page) into:

[self.emailWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('username').value = '%@';", user]];
[self.emailWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('password').value = '%@';", pw]];
[self.emailWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('input_LoginPage-ipad_1').click();"]];

Solved it.

Thanks for all your help folks.

You have to put the javascript code inside one NSString and then execute with stringByEvaluatingJavaScriptFromString. This is how the code would look like:

 [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('email').value = '%@'", email]];
 [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('pass').value = '%@'", password]];

Use this Code This will Work

view.loadUrl("javascript:(function() { "
    + " document.getElementById('login').value = '" + emailAdress+ "';"
    + " document.getElementById('login').readOnly='true'"
    + "})();");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!