【发布时间】:2019-01-19 14:22:35
【问题描述】:
背景
- 我正在使用htmlUnit来模拟用户在某个页面中的行为
- 我正在进入需要输入用户凭据的登录页面
问题:
我想填写详细信息的表单会动态更改并推送新的输入字段,其值会随着插入的每个字符而变化。
此输入字段有几个事件侦听器,据我从 Chrome 调试中发现,按键事件与我最相关,因为这最终会生成更新值
-
页面“加载”时出现以下错误: [User1st] 提取语言代码时出错 TypeError: Cannot call method "getAttribute" of undefined
4.c.g.h.javascript.StrictErrorReporter : runtimeError: message=[指定了无效或非法的选择器(选择器:'*,:x' 错误:无效的选择器:*:x)。] sourceName= https://???/jquery-1.10.2.min.js] line=[ 3] lineSource=[null] lineOffset=[0]
一些代码:
WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setRedirectEnabled(true);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.waitForBackgroundJavaScript(5000);
final HtmlPage page = webClient.getPage(WEBSITE_URL);
HtmlForm loginForm = page.getFormByName("login");
HtmlTextInput userIdField = loginForm.getInputByName("USERID");
HtmlPasswordInput passwordField = loginForm.getInputByName("USERPASSWORD");
userIdField.type("ID");
passwordField.setText("PASSWORD");
我接下来要做的只是迭代表单输入字段并查看它们的值。
如何确保所有相关的 js 代码都真正执行?
【问题讨论】:
-
请发布 WEBSITE_URL 以便我们重现您的案例。
-
最终使用硒
标签: java web-scraping htmlunit