如何填写Web视图与Java和JavaScript领域的机器人 [英] How to fill fields in web view in android with java and javascript

查看:160
本文介绍了如何填写Web视图与Java和JavaScript领域的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图填补某些领域在我的Andr​​oid 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]

这篇关于如何填写Web视图与Java和JavaScript领域的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆