21

I have a website with href in it which redirected me to https

<a id="mA" href="javascript:pLogin(2)" class="login-link__link private-cab-link"><i class="icon-user"></i>Авторизация</a>

So, I can click on it by JavaScript. It works in chrome console

javascript:(function(){document.getElementById('mA').click();})()

Now I'm trying to do the same in WebView by clicking my app's button.

public class RostelecomLoginActivity extends Activity {

    WebView webView;
    String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.activity_rostelecom_login);
        Intent webIntent = getIntent();
        String url = webIntent.getStringExtra("url");

        webView = (WebView) findViewById(R.id.webView1);
        webView.setWebViewClient(new MeWebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSaveFormData(true);
        webView.getSettings().setSavePassword(true);
        webView.loadUrl(url);

        Button buttoner = (Button) findViewById(R.id.button1);
        buttoner.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                webView.loadUrl("javascript:(function(){document.getElementById('mA').click();})()");
            }
        });

    }

}

I'm using MyWebViewClient to allow all certificates

public class MeWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();
    }
}

The js injection doesn't work. If I click on href in WebView it works. What can be wrong?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Valeriy
  • 785
  • 2
  • 10
  • 28
  • 1
    please consider to accept an answer. The solution provided by "deviato" works great. I tested it. Regards – Lisitso Jun 06 '14 at 11:06

2 Answers2

35

click() isn't implemented in android js interface, you have to use HTML DOM Event Object, like this:

webView.loadUrl("javascript:(function(){"+
    "l=document.getElementById('mA');"+
    "e=document.createEvent('HTMLEvents');"+
    "e.initEvent('click',true,true);"+
    "l.dispatchEvent(e);"+
    "})()");
deviato
  • 2,027
  • 1
  • 15
  • 9
  • 3
    God bless you! You helped me a lot after many days spent finding out the solution. Thank you so much – Lisitso Jun 06 '14 at 07:45
  • @deviato: hi, what if button/href do not have ID? could you please give a solution. in my example `code ` Thanks – JSB Mar 28 '16 at 23:00
  • 1
    @JasbirBhinder in that case is simply a javascript problem that you can solve by using `getElementsByClassName` or `getElementsByName`, which return an array of objects. For example, if you have only one element with that name, you can use `"l=document.getElementsByName('initCaseSearch');"+` at the place of the second line. – deviato Apr 15 '16 at 17:15
  • @deviato is there anyway to get an element by only their value. In my case there is nothing buy type and value. Thanks. – LUKER Feb 03 '18 at 20:18
  • @deviato `` could you provide me solution for this. I want to https://www.incometaxindiaefiling.gov.in/home and press login – Rohit Nov 19 '18 at 07:53
11

You'll have to add a javaScript interface to the WebView to call a JavaScript function from android code.

Try something like this:-

    Button buttoner = (Button) findViewById(R.id.button1);
    buttoner.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            JavascriptInterface javasriptInterface = new JavascriptInterface(RostelecomLoginActivity.this);
            webView.addJavascriptInterface(javasriptInterface, "MyInterface");
            webView.loadUrl("javascript:(function(){document.getElementById('mA').click();})()");
        }
    });
bakriOnFire
  • 2,685
  • 1
  • 15
  • 27
  • I use api level 17, in documentation I saw that I should add annotation. But if I add '@JavascriptInterface' before public void onClick, it still doesn't work cause I can't create JavascriptInterface jsint = new JavascriptInterface(RostelecomLoginActivity.this); Cannot instantiate the type JavascriptInterface (error) – Valeriy May 23 '13 at 14:22
  • 1
    @bakriOnFire I don't think you understand what `addJavascriptInterface` does – njzk2 Sep 10 '16 at 02:20
  • @bakriOnFire Is there anyway to get an element by only their value? There is no id or class. – LUKER Feb 03 '18 at 20:20
  • `` could you provide me solution for this. I want to [incometaxindiaefiling.gov.in/home] and press login – Rohit Nov 19 '18 at 08:04