2

I am trying to login to a website programmatically using JavaScript.This is the HTML code of login form.

   <form action="https://www.example.com/login/index.php" method="post" id="login" autocomplete="off" >
      <div class="loginform">
        <div class="form-label"><label for="username">Username</label></div>
        <div class="form-input">
          <input type="text" name="username" id="username" size="15" value="" />
        </div>
 <div class="form-label"><label for="password">Password</label></div>
        <div class="form-input">
          <input type="password" name="password" id="password" size="15" value="" autocomplete="off" />
          <input type="submit" id="loginbtn" value="Login" />
        </div>
      </div>

I tried to login the website using webview in android application.I used

  webView.loadUrl("http://www.example.com/info");
    webView.setVisibility(View.INVISIBLE);
    webView.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
            webView.loadUrl("javascript: {" +
                    "document.getElementById('username').value = '"+user+"';" +
                    "document.getElementById('password').value = '"+pass+"';" +
                    "document.getElementById('loginbtn').click();" +
                    "};");  
        }

        public void onPageFinished2(WebView view, String url) {
            webView.loadUrl(url);
        }       
    });

    webView.setVisibility(View.VISIBLE);
    webView.clearCache(true);
    webView.clearHistory();
}

But the webview only displayed uesrname and password in input fields.No button click happened.What's wrong with my code?

2
  • You can manually login to the website right? Commented Sep 10, 2014 at 7:35
  • Yeah,I can manually login. Commented Sep 10, 2014 at 8:56

3 Answers 3

1

Change your code as follows

webView.loadUrl("http://www.example.com/info");
webView.setVisibility(View.INVISIBLE);

//add following two lines
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);

webView.setWebViewClient(new WebViewClient() {

    public void onPageFinished(WebView view, String url) {
        webView.loadUrl("javascript: {" +
                "document.getElementById('username').value = '"+user+"';" +
                "document.getElementById('password').value = '"+pass+"';" +
                "document.getElementById('loginbtn').click();" +
                "};");  
    }

    public void onPageFinished2(WebView view, String url) {
        webView.loadUrl(url);
    }       
});

webView.setVisibility(View.VISIBLE);
webView.clearCache(true);
webView.clearHistory();
}

You can learn more from this link.

Sign up to request clarification or add additional context in comments.

5 Comments

I tried as you said.It still gives me the login page.But now, it does not show values in the input fields.When I'm trying to run the application second time login page shows session time expired message.Does it mean that I have logged into the site in the first time? If so, why it does not perform logging task?
I tried it too.But it still staying in the login page.I have edited my full code.please take a look at it and say is there any problem within my code?
try to add WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true);. And also why do you need onPageFinished2 method?
I want to go the .../info page.So I first load that url.But the website requires a login.So it redirects to the login page.right? That's why I used onPageFinished method to login programmatically.I have enabled JavaScript as you said earlier.Why the webview does not update within new page? It is still staying in the login page.
if you share what solved the problem as an answer and tick it as an accepted answer it will help the people who are stuck with a similar situtation.
0

It's because this...

document.getElementByName('loginbtn').onclick();

it's supposed to be this...

document.getElementByName('loginbtn').click();

There's no such onclick method in a HTMLElementObject object in javascript, the method you're trying to call is click. Here's the documentation from W3Schools

1 Comment

Tried.But still staying at the login page.no progress.
0
document.getElementByName('loginbtn').click(); 

worked for me. The problem was I have overridden public boolean shouldOverrideUrlLoading(WebView view, String url) method in WebViewClient Class.

After I removed that part this worked perfectly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.