gpt4 book ai didi

java - 使用 Jsoup 登录网页

转载 作者:行者123 更新时间:2023-12-02 04:42:18 25 4
gpt4 key购买 nike

我正在开发一个应用程序,可以登录到给定的网站并从中获取一些信息,但我尝试无法确定它是否已登录。请帮我解决这个问题!

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());

}}

先尝试调试代码几次,以便更好地理解。

关于java - 使用 Jsoup 登录网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56509318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com