How to execute JavaScript code after loading a page in webview?

I need to send data via JavaScript to webview. After the page loads, I need to get the value of a given field.

It's working, but I have to click the button again as soon as the page loads in the webviwer.

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

webView = (WebView) findViewById(R.id.webViewId);

textView1 = (EditText) findViewById(R.id.textView1Id);

textView2 = (EditText) findViewById(R.id.textView2Id);

textView3 = (TextView) findViewById(R.id.textView3Id);

button = (Button) findViewById(R.id.bt1Id);

webView.loadUrl("https://site.com.br");

WebSettings webSettings = webView.getSettings();

webSettings.setJavaScriptEnabled(true);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

String executaScript1 = "document.getElementById('campoLogin').value = 'admin';" +

"document.getElementById('campoSenha').value = 'admin';" +

"document.getElementsByName('button')[0].click();";

String executaScript2 = "document.getElementById('disponivel').value;";

webView.loadUrl("JavaScript:" + executaScript1);

webView.evaluateJavascript("JavaScript:" + executaScript2, new ValueCallback() {

@Override

public void onReceiveValue(String...

Read More »

By: StackOverFlow - 6 days ago

Related Posts