如何填写Web视图与Java和JavaScript领域的机器人
我试图填补某些领域在我的Android Web视图。但所有的时间我有这个错误 5月1日至28日:06:22.980:E / Web控制台(2827):未捕获类型错误:在空无法设置为null财产价值:1
I am trying to fill some fields in my android web view. but all the time I have this error 01-28 05:06:22.980: E/Web Console(2827): Uncaught TypeError: Cannot set property 'value' of null at null:1
我已经搜查,但没有找到一些解决方案。我怎样才能解决这个问题?
这里是我的code
I have searched but didnt find some solution. How can I resolve this? Here is my code
public void onStart() {
super.onStart();
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
String url = "file:///android_asset/index.html";
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript: {"
+ "document.getElementById('AddSerialNum').value = '"
+ ABC
+ "';"
+ "document.getElementById('ctl00_PageContent_lblName').value = '"
+ ABC + "';};");
}
});
}
这是我的index.html文件
this is my index.html file
<!DOCTYPE html>
<html>
<body>
<div style="BORDER-BOTTOM: #dfdfdf 3px solid; PADDING-BOTTOM: 1px; MARGIN-BOTTOM: 10px; HEIGHT: 25px;">
......
<div style="padding: 10px;">
<table width="100%">
<tbody><tr>
<td>
<div style="float: left;">
<div>
<form>Serial Number: <input type="text" name="AddSerialNum"></form><br><br>
<span id="ctl00_PageContent_lblName" style="font-weight:bold;"></span>
</div>......
有什么办法满山遍野,后来用java获取数据?
Is there any way to fill the fields and get data later with java?
先谢谢了。
通过的getElementById
您将通过得到一个DOM元素ID
。该ID AddSerialNum
不存在,这是什么错误告诉你。
你有一个输入
与 NAME =AddSerialNum
,其中,我想你想要得到的。无论是添加 ID
来,或使用 getElementsByName(AddSerialNum)[0]
With getElementById
you will get an DOM Element by id
. The id AddSerialNum
does not exist, this is what the error tells you.
You have an input
with name="AddSerialNum"
, which, i think you want to get. Either add an id
to it, or use getElementsByName("AddSerialNum")[0]
修改:
就像我已经指出的那样,你必须使用这样的:
EDIT: Like i already pointed out, you have to use this:
webView.loadUrl("javascript: {document.getElementsByName(\"AddSerialNum\")[0].value ='" + ABC + "';};");
的 getElementsByName
返回元素的数组,你必须得到第一个与 [0]
The getElementsByName
returns an array of elements, you have to get the first one with [0]