预填充WebView文本字段 [英] Pre-fill WebView text fields
问题描述
我需要预先填写 UIWebView
中的文本字段,并了解 javascript
是最好的如何解决这个问题。不幸的是,我对 javascript
一无所知,并且在过去的几个小时里一直在摸索,无处可去。
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.
最新的拙劣尝试:
- (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 -
-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');"]];
}
警报出现但ID password_5
未填写。
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 -
-EDIT 2-
我很确定问题与目标网站上的HTML /嵌套有关,因为我刚刚在另一个网站上尝试过这个问题并且工作正常。
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.
目标网站嵌套如下:
<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>
推荐答案
我在Firefox中突出显示了元素ID并选择检查元素
。
原来这给出了返回的ID不同的ID:
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);
将这些ID(显然根据用于导航到页面的内容而变化)插入: / p>
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();"]];
解决了这个问题。
谢谢大家你的帮助伙伴。
Thanks for all your help folks.
这篇关于预填充WebView文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!