java⾃动填表_java有没有⾃动填写表单提交?HtmlUnit⾃动填写表单并提交package ebay;
import java.io.IOException;
import java.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.setAjaxController(new NicelyResynchronizingAjaxController());// 设置Ajax异步
//登录
try {
HtmlPage page = (HtmlPage) Page(sUrl);
HtmlForm Forms().get(0);//FormByName("");
HtmlTextInput txtUName = (HtmlTextInput )InputByName("UserName"); //⽤户名text框
txtUName.setValueAttribute("name");
HtmlPasswordInput txtPwd = (InputByName("Password");//密码框txtPwd.setValueAttribute("password");
//submit没有name,只有class和value属性,通过value属性定位元素
HtmlSubmitInput submit=(HtmlSubmitInput) InputByValue("登 录");
page = (HtmlPage) submit.click();//登录进⼊
webClient.waitForBackgroundJavaScript(10000);//等待1秒
System.out.println(page.asText());
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
htmlbutton属性}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论