描述:我想用java(js)技术实现一个表单提交的操作,打开某表单网页,用java对表单进行填充,然后提交。(其他站点)有点跨域的意思。没有想到实现的思路,求教各位大神指点实现这种功能的思路。谢谢!目标网页是其他任何表单网页。
2 回答
小怪兽爱吃肉
TA贡献1852条经验 获得超1个赞
HtmlUnit自动填写表单并提交
package ebay; import java.io.IOException; import java.net.MalformedURLException; import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.DomElement; import com.gargoylesoftware.htmlunit.html.DomNodeList; import com.gargoylesoftware.htmlunit.html.HtmlButton; import com.gargoylesoftware.htmlunit.html.HtmlButtonInput; import com.gargoylesoftware.htmlunit.html.HtmlForm; import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput; import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; import com.gargoylesoftware.htmlunit.html.HtmlTextInput; public class GGManage { public static void main(String[] args){ String sUrl="IP";//网址 //webclient设置 WebClient webClient = new WebClient(BrowserVersion.CHROME); //创建一个webclient webClient.getOptions().setJavaScriptEnabled(true); // 启动JS webClient.getOptions().setUseInsecureSSL(true);//忽略ssl认证 webClient.getOptions().setCssEnabled(false);//禁用Css,可避免自动二次请求CSS进行渲染 webClient.getOptions().setThrowExceptionOnScriptError(false);//运行错误时,不抛出异常 webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); webClient.setAjaxController(new NicelyResynchronizingAjaxController());// 设置Ajax异步 //登录 try { HtmlPage page = (HtmlPage) webClient.getPage(sUrl); HtmlForm form=page.getForms().get(0);//page.getFormByName(""); HtmlTextInput txtUName = (HtmlTextInput )form.getInputByName("UserName"); //用户名text框 txtUName.setValueAttribute("name"); HtmlPasswordInput txtPwd = (HtmlPasswordInput)form.getInputByName("Password");//密码框 txtPwd.setValueAttribute("password"); //submit没有name,只有class和value属性,通过value属性定位元素 HtmlSubmitInput submit=(HtmlSubmitInput) form.getInputByValue("登 录"); page = (HtmlPage) submit.click();//登录进入 webClient.waitForBackgroundJavaScript(10000);//等待1秒 System.out.println(page.asText()); }catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
添加回答
举报
0/150
提交
取消