使用Jsoup登录网页
Login to webpge using Jsoup
我正在开发一个可以登录给定网站并从中获取一些信息的应用程序,但我试过无法弄清楚它是否已登录。请帮我解决这个问题!
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);
使用 Selenium WebDriver 进行此操作可能真的很有效,
请确保您正确设置了网络驱动程序路径,如果有任何其他问题,请随时发表评论。
以下是登录表单的代码,用于获取您点击登录后显示在表单上的错误消息。
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());
}}
先尝试调试代码几次,以便更好地理解。
我正在开发一个可以登录给定网站并从中获取一些信息的应用程序,但我试过无法弄清楚它是否已登录。请帮我解决这个问题!
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);
使用 Selenium WebDriver 进行此操作可能真的很有效,
请确保您正确设置了网络驱动程序路径,如果有任何其他问题,请随时发表评论。
以下是登录表单的代码,用于获取您点击登录后显示在表单上的错误消息。
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());
}}
先尝试调试代码几次,以便更好地理解。