如何使用Jsoup单击javascript按钮?
String html = Jsoup.connect(url)
.followRedirects(true)
.data("login_name", username)
.data("password", userpassword)
.method(Method.POST).get().html();
这是我用来在网站中设置用户名和密码字段的代码,但是当我抓取html时,它会为我提供登录页面的html,其中填写了该页面的字段,而不是登录后的页面。因此,这必须意味着Jsoup只是填写了字段,但没有登录我。我将如何去做?另外,登录按钮没有id元素,也没有name元素。它只有JavaScript。抱歉,如果我不清楚,我是新手。这是表单的html代码:
<form name="form" id="form" method="POST" action="/portal/login?etarget=login_form" autocomplete="off">
这是登录按钮的html代码:
<a href="javascript:document.form.event_override.value='login';document.form.submit();" class="btn_css">
如何使用Jsoup“单击”登录按钮?另外,我尝试使用.post()方法而不是.method(Method.POST),但是当我这样做时,我的程序无法正常工作,并显示以下消息:“错误:加载URL时出错400”
另外,我也不拥有该网站,并且正在为Android开发的本机应用程序中使用它。
最佳答案
当在connect方法中传递一个URL时,而不是传递登录页面URL,而是传递主页链接。
Document doc = Jsoup.connect("http://www.website.com/home.php")
.data("email", "myemailid")
.data("pass", "mypassword")
// and other fields which are being passed in post request.
.userAgent("Mozilla")
.post();
试试这个,您将得到结果。