programmatic click in Android WebView

前端 未结 2 1787
遥遥无期
遥遥无期 2020-12-01 11:27

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



        
相关标签:
2条回答
  • 2020-12-01 11:39

    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);"+
        "})()");
    
    0 讨论(0)
  • 2020-12-01 11:49

    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();})()");
            }
        });
    
    0 讨论(0)
提交回复
热议问题