Java和HTMLUnit:如何点击提交按钮? [英] Java and HTMLUnit: How to click on submit button?

查看:834
本文介绍了Java和HTMLUnit:如何点击提交按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是全新的Java,需要编写各种Java应用程序来进行网页抓取和网页交互。



我开始使用Selenium,但是因为它直接与一个浏览器,对我来说是不实际的。



我需要执行以下任务:
1.转到特定的URL
2在输入栏输入一个邮政编码
3.点击提交按钮
4.解析并保存特定div标签或重新查询页面的结果。



我正在使用HTMLUnit和Eclipse。
我可以访问一个网页,并通过引用表单,然后输入名称在输入中输入一个邮政编码。
但是,当我尝试点击提交按钮时,我会收到一个ElementNotFoundException错误。



以下是在页面上如何实现提交按钮的示例:

 
type =submitvalue =submitname =submit>输入邮政编码

这是我的代码如下:

  package htmlunittest; 

import java.io.IOException;
import java.net.URL;
import junit.framework.TestCase;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.RefreshHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlButtonInput;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

public class htmlunittest extends TestCase {

@SuppressWarnings(deprecation)
public static void main(String [] args)throws Exception
{

final WebClient webClient = new WebClient();
final HtmlPage startPage = webClient.getPage(http://www.testpage.com);

final HtmlForm form =(HtmlForm)startPage.getForms()。get(2);

final HtmlTextInput textField = form.getInputByName(address);
textField.setValueAttribute(我的邮政编码);

// throws ElementNotFoundException
final HtmlSubmitInput button = form.getInputByName(submit);

//现在通过单击按钮提交表单并返回第二页。
final HtmlPage page2 = button.click();
System.out.println(page2.getHtmlElementById(mainContent));

webClient.closeAllWindows();

}
}

有人可以指出我正确的方向是怎样通过HTMLUNIT点击提交按钮?



谢谢

解决方案

这是一个很难找出为什么在没有你想要的整个页面不工作。



我敢打赌你是没有得到正确的形式与 .get(2),顺便说一下,通常是一个坏主意,以获取表单的方式,因为如果目标页面稍微改变其源代码只是为了添加一个表单,您的刮刀将无法再次工作,因为索引将会不同。


I am brand new to Java and need to write various java applications to do web scraping and web page interaction.

I started using Selenium but because it interacts directly with a browser, it is not practical for my use.

I need to do the following tasks: 1. Go to a specific URL 2. Enter a post code in a input field 3. Click submit button 4. Parse and save results from specific div tag or re-query page.

I am using HTMLUnit and Eclipse. I can access a webpage and enter a post code in an input by referencing the form and then the input name. However when I try to click the submit button, I get an ElementNotFoundException error.

Here is a sample of how the submit button is implemented on the page:

 type="submit" value="submit" name="submit">Enter post code

Here is what my code looks like:

package htmlunittest;

import java.io.IOException;
import java.net.URL;
import junit.framework.TestCase;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.RefreshHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlButtonInput;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

public class htmlunittest extends TestCase{

@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception 
{

    final WebClient webClient = new WebClient();            
    final HtmlPage startPage = webClient.getPage("http://www.testpage.com");

    final HtmlForm form = (HtmlForm) startPage.getForms().get(2);

 final HtmlTextInput textField = form.getInputByName("address");
  textField.setValueAttribute("my post code");

//throws ElementNotFoundException
  final HtmlSubmitInput button = form.getInputByName("submit");

// Now submit the form by clicking the button and get back the second page.
final HtmlPage page2 = button.click();
System.out.println(page2.getHtmlElementById("mainContent"));

webClient.closeAllWindows();

}
}

Can someone please point me in the right direction as to how I should click on the submit button via HTMLUNIT?

Thanks

解决方案

It is a bit difficult to find out why that is not working without the whole page that you're trying to fetch.

I bet you are not getting the right form with the .get(2), which by the way, is usually a bad idea to get forms that way because if the target page slightly changes its source code just to add a form above that one your scraper won't work again because the index will be different.

这篇关于Java和HTMLUnit:如何点击提交按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆