我正在开发一个应用程序,该应用程序可以登录到给定的网站并从中获取一些信息,但是我尝试不知道是否已登录。请帮助我!
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);
答案 0 :(得分:0)
使用Selenium WebDriver进行此操作可能确实有效,
请确保您正确设置了Web驱动程序路径,如果还有其他任何意见,请随时发表评论。
以下是登录表单的代码,并在您单击登录后获取显示在表单上的错误消息。
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());
}}
尝试几次调试代码,以更好地理解。