简体   繁体   中英

Login to webpge using Jsoup

I was developing an app that would login to the given website and take out some info from that but I tried can't figure out if it is logged in or not. Please help me with this!

final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36";
    //final String LOGIN_FORM_URL = "http://103.253.211.190/GLBBlackBoard/Home/default.aspx";
    final String USERNAME = "0171cs074";
    final String PASSWORD = "dedsec";

  //Go to login page
    Connection.Response login = Jsoup.connect(LOGIN_FORM_URL)
                                     .method(Connection.Method.GET)
                                     .userAgent(USER_AGENT)
                                     .execute();
    // find form

    FormElement loginForm = (FormElement)login.parse().select("form").first();
  checkElement("Login Form", loginForm);
    //usernsme

  Element loginField = loginForm.select("input#username.normal").first();
  checkElement("Login Field", loginField);
  loginField.val(USERNAME);

    //pass

  Element passwordField = loginForm.select("input#password.normal").first();
  checkElement("Password Field", passwordField);
  passwordField.val(PASSWORD);


  //login
  Connection.Response loginActionResponse = loginForm.submit()
          .cookies(login.cookies())
          .userAgent(USER_AGENT)
          .execute();

  System.out.println(loginActionResponse.parse());
}

private static void checkElement(String name, Element elem) {
    if (elem == null) {
        throw new RuntimeException("Unable to find " + name);

Using Selenium WebDriver for this operation might be really effective,

Please make sure you set the web driver path correctly & if there is anything else feel free to comment.

following is the code for the login form & to fetch the error message displayed on the Form after you hit log in.

public class GLBBlackBoard {

public static void main(String[] args) throws InterruptedException {

    System.setProperty("webdriver.chrome.driver", "D:\\Tushar\\JARs\\selenium\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    driver.get("http://103.253.211.190/GLBBlackBoard/Home/default.aspx");
    driver.manage().window().maximize();

    final String username = "0171cs074";
    final String password = "dedsec";

    List<WebElement> ele = driver.findElements(By.cssSelector("input.inputbox"));
    WebElement w1 = ele.get(0);
    WebElement w2 = ele.get(1);

    w1.sendKeys(username);
    Thread.sleep(1000);
    w2.sendKeys(password);
    Thread.sleep(1000);

    WebElement login = driver.findElement(By.id("ctl00_ctl00_ContentPaneLeft_ctlLogin1_Button1"));
    login.click();

    WebElement msg = driver.findElement(By.id("ctl00_ctl00_ContentPaneLeft_ctlLogin1_lblMessage"));

    System.out.println(msg.getText());

}}

try debugging the code first few times, for better understanding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM