Pre-fill WebView text fields
ios, javascript, objective-c, uiwebview
Solution
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.
Problem
I need to pre-fill text fields in a `UIWebView` and am given to understand that `javascript` is the best way to go about this. Unfortunately I know nothing of `javascript` and have been fumbling about for the last few hours, getting nowhere. Latest botched attempt: ``` - (void)webViewDidFinishLoad:(UIWebView *)webView { // username field id = username_5 & pw field id = password_5 NSString *javascript = @"\ var user = 'testUser';\ var pw = 'testPW';\ document.getElementById('username_5').value = user; \ document.getElementById('password_5').value = pw; \ ;"; // Execute JS [_emailWebView stringByEvaluatingJavaScriptFromString:javascript]; } ``` Can anyone point me in the right direction? -EDIT- I also tried tried delaying the call in case the page had not fully loaded and if I call something like: ``` - (void)webViewDidFinishLoad:(UIWebView *)wv { [NSObject cancelPreviousPerformRequestsWithTarget:self]; [self performSelector:@selector(injectJavascript) withObject:nil afterDelay:1.0]; } - (void)injectJavascript { [self.emailWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('password_5').value = 'testPW';"]]; [self.emailWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.alert('test');"]]; } ``` the alert appears but the field with ID `password_5` is not filled. The fields in question are nested within a form. Don't know if that makes any difference? -EDIT 2- I'm pretty sure the problem is related to the HTML / nesting on the target website as I just tried this on another site and it worked. The target site is nested like this: ``` <html> <head> … </head> <body onload="FinishLoad(1);hideJSWarn();"> <div id="noJSWarn" class="cssSecurityWarning" style="display: none;"> … </div> <table id="table_LoginPage_1" > … </table> <table id="table_LoginPage_2" > … </table> <blockquote> <form id="frmLogin_4" onsubmit="return Login(1)" autocomplete="off" method="POST" action="login.cgi" name="frmLogin"> <input id="tz_offset_5" type="hidden" name="tz_offset"></input> <table id="table_LoginPage_3" > <tbody> <tr> … <td valign="top"> <table id="table_LoginPage_6" > <tbody> <tr> <td> … </td> <td> <input id="username_5" type="text" size="20" name="username"></input> ```