- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试获取网页。我正在获取表单、文本输入、复选框和提交按钮,以便我可以通过 java 代码填充这些内容。
首先,我收到这些警告(我认为 ScriptEngine 无法加载某些脚本):
oct 18, 2015 9:45:01 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'application/x-javascript'.
oct 18, 2015 9:45:01 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'application/x-javascript'.
oct 18, 2015 9:45:01 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'application/x-javascript'.
无论如何,在我正确填写java输入并在提交按钮上调用方法click()之后,我没有得到提交后应该加载的页面。那么,我错过了什么?
这是 html 代码:
<form name="form" method="post" action="Login.aspx?test=1" onsubmit="javascript:return doSomething_OnSubmit();" id="form">
//then there are some hidden inputs
//...
<input name="tax_code" type="text" maxlength="10" id="tax_code" style="color:Red;width:120px;" />
<input id="privacy" type="checkbox" name="privacy" onclick="activeConfirmButton()" />
//initially the confirm button is deactivated, after the checkbox is checked the confirm button is active with the onclick event added on it.
<input type="submit" name="Confirm" value="Confirm" onclick="javascript:Form_DoPostBack(new Form_DoPostBack())" id="Confirm" style="color:Blue;font-family:calibri;width:150px;Z-INDEX: 0" />
这是java代码:
try (final WebClient webClient = new WebClient(BrowserVersion.CHROME))
{
/* turn off htmlunit warnings */
//java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);
//webClient.getOptions().setActiveXNative(true);
//webClient.waitForBackgroundJavaScript(50000);
// Get the first page
final HtmlPage page1 = webClient.getPage("http://example.com/examples/Login.aspx?test=1");
final HtmlForm form = page1.getFormByName("form");
final HtmlTextInput taxCodeTextField = form.getInputByName("tax_code");
final HtmlCheckBoxInput checkboxInput = form.getInputByName("privacy");
final HtmlSubmitInput confirmButton = form.getInputByName("Confirm");
//Setting textfield and checkbox
taxCodeTextField.setValueAttribute("TAX_CODE");
checkboxInput.setChecked(true);
//onclick of the checkbox, to activate the confirm button
checkboxInput.click();
// onclick of the confirm button
final HtmlPage page2 = confirmButton.click();
WebResponse response = page2.getWebResponse();
String content = response.getContentAsString();
System.out.println("HTML SOURCE: "+content);
}
catch(Exception e){
}
最佳答案
有一些要点需要考虑。
.setChecked(true)
和 .click()
onclick
处理程序中的 JavaScript setTimeout()
显示,因此必须获得一个新页面。下面的代码更新页面并返回结果:
try (final WebClient webClient = new WebClient(BrowserVersion.CHROME)) {
// disable caching
webClient.getCache().setMaxSize(0);
// Get the first page
final HtmlPage page1 = webClient.getPage(url);
final HtmlForm form = page1.getFormByName(formName);
final HtmlTextInput taxCodeTextField = form.getInputByName(taxCodeTextFieldName);
HtmlCheckBoxInput checkboxInput = form.getInputByName(checkboxInputName);
taxCodeTextField.type(taxCode);
checkboxInput.click();
//wait a little
Thread.sleep(2000);
//get the main page
HtmlPage page2 = (HtmlPage) webClient.getTopLevelWindows().get(0).getEnclosedPage();
HtmlSubmitInput confirmButton = page2.getFormByName(formName).getInputByName(confirmButtonName);
final HtmlPage page3 = confirmButton.click();
System.out.println(page3.asText());
}
关于java - Java中使用API HtmlUnit登录并获取网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33196528/
htmlunit 报告从 css 到页面上解析错误的所有内容。 这个怎么静音?? 最佳答案 把这些放在声明 webClient 之后 webClient.setCssError
可能显示 Javascript 测试支持 package htmlunitpoc; import com.gargoylesoftware.htmlunit.WebClient; import co
我正在尝试使用 HtmlUnit 登录到我的本地 wordpress 网站,但它似乎存在 cookie 问题。 那是代码的开头: WebClient webClient = new WebClient
有什么办法可以在 HtmlUnit 中使用 session 更改 url? 我的情况如下所示, 登录 http://test.raja.com与凭据。 获取页面http://home.raja.com
我有以下代码: WebClient webClient = new WebClient(); HtmlPage page = webClient.getPage("http://www.myland.
我研究过htmlunit、httpunit、jwebunit、selenium等ui测试工具。 我对测试工具不是很熟悉。 Htmlunit 在 javascript 支持方面听起来是个不错的选择。然后
我拥有的页面对象是 click() 长链的结果调用(以防万一您想知道如何在不知道用于获取它的 URL 的情况下拥有一个页面对象)。 方法HtmlPage.getDocumentURI未实现。 Html
这是我要抓取的页面:https://www.tokopedia.com/berkahcell2/promo-termurah-vr-virtual-reality-box-v-2-0-remote-b
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
我的站点结构看起来像这样: Item 1 Desc 1
我的代码是这样的: WebClient webClient = new WebClient(BrowserVersion.CHROME); webClient.setAjaxController(ne
我只希望页面的文本内容,并且希望抓取尽可能轻巧。我可以关闭HTMLUnit开箱即用的所有JavaScript和CSS以及其他外部内容的解析和其他加载功能吗? 最佳答案 我认为与您正在寻找的最接近的东西
我有一个提交按钮,但无法点击.. Send SMS 我已经尝试过这个: page = (HtmlPage) form.getInputByValue("Send SMS").click()
我使用 HtmlUnit 对我的网站进行自动化测试。我的网站使用 gmaps api - 发送外部网站请求需要花费大量时间(我有数百次测试和数千次页面加载)。 我需要一些方法来告诉 HtmlUnit
我使用 HtmlUnit 对我的网站进行自动化测试。我的网站使用 gmaps api - 发送外部网站请求需要花费大量时间(我有数百次测试和数千次页面加载)。 我需要一些方法来告诉 HtmlUnit
我正在使用 HtmlUnit 加载一个充满 JavaScript 的网页。 WebClient 可以很好地执行 JavaScript。 但是,当我单击调用 Ajax 调用的特定表单上的按钮时,即使调用
我正在尝试使用 HtmlUnit 登录 Facebook 页面并查看其 HTML 内容。我正在尝试通过 HtmlUnit 填写登录凭据,但在单击提交按钮时我没有看到正在执行的 session 。 在
Hy...我想使用 HtmlUnit 登录到一些 3rd 方网站。但是 HtmlUnit 应该能够告诉我对输入站点的登录尝试是否成功。有什么办法可以使用 HtmlUnit 执行此任务。请帮忙 ..!!
我正在从 selenium-1 升级到 selenium-2 并尝试新的 HtmlUnit 驱动程序。我已经尝试了一些基本的测试(打开一个页面,get_text,..),它似乎 极慢(我认为 chro
我正在使用 HTMLUnit。我正在访问页面,但是特殊(马耳他)字符显示错误。例如,ġuvni 显示为 ?uvni HtmlPage page = submit_button.click(); Sys
我是一名优秀的程序员,十分优秀!