在 Android Webview 中以编程方式填写表单 - JavaScript [英] fill form programmatically in Android Webview - JavaScript
问题描述
我正在尝试自动填写我学校网站上的表格.我已经看到了一些使用 javascript 的方法.
I'm trying to automatically fill a form from the website of my school. I've seen some ways to do with javascrip.
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navegador_siiau_layout);
navegador_siiau = (WebView) findViewById(R.id.wv_navegador_siiau);
navegador_siiau.getSettings().setJavaScriptEnabled(true);
navegador_siiau.loadUrl("http://siiauescolar.siiau.udg.mx/wus/gupprincipal.inicio");
navegador_siiau.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
String user="207512134";
String pwd="FENMAA";
view.loadUrl("javascript:document.getElementsByName('p_codigo_c').value = '"+user+"';document.getElementsByName('p_clave_c').value='"+pwd+"';");
}
});
}
结果是一个空白页,其中包含我尝试放入表单中的最后一个字符串...我该怎么做才能正确填写和提交表单?
The result is a blank page with the last string I try to put in the form... What can I do to fill and submit the form correctly?
推荐答案
这是未经测试的,但我发现这里有两个错误:
This is untested, but I see two things wrong here:
在调用
loadUrl
之前,您应该使用WebViewClient
实现调用setWebViewClient
.然而,loadUrl
是异步的,所以它可能不会有什么不同,但我认为这值得一试.
You should call
setWebViewClient
with yourWebViewClient
implementation before you callloadUrl
. However,loadUrl
is asynchronous, so it probably would not make a difference, but this is worth a try I think.
您没有在 onPageFinshed 中调用 super.onPageFinished(view, url);
.您应该添加此调用.
You are not calling super.onPageFinished(view, url);
in onPageFinshed. You should add this call.
编辑 2:
我终于看到了您正在使用的实际页面.问题是页面在不同的框架中加载内容,因此您实际上必须对不同的文档进行 getElementsByName
调用.您想要的两个输入所在的框架在您的页面中具有名称 mainFrame
.因此,您应该像上面那样编写 javascript 并将其加载到 WebView
中:
I finally took a look at the actual page you are working with. The problem is that the page loads content in a different frame, so you actually have to make the getElementsByName
call on a different document. The frame in which both of the inputs you want are located has the name mainFrame
in your page. So, you should write the javascript like this and load that into the WebView
as you have done above:
window.frames["mainFrame"].document.
getElementsByName('p_codigo_c')[0].value = "user";
window.frames["mainFrame"].document.
getElementsByName('p_clave_c')[0].value = "password";
这篇关于在 Android Webview 中以编程方式填写表单 - JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!