如何使用 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),但是当我这样做时,我的程序没有运行,并给了我这条消息:“错误:400 加载 URL 错误”
此外,我不拥有该网站,我在为 Android 构建的 native 应用程序中使用它。
最佳答案
在连接方法中传递 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();
试试这个,你会得到你的结果。
关于javascript - Jsoup javascript按钮点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8721364/