java控制html按钮,java - HtmlUnit button click - Stack Overflow

I'm trying to send a message on www.meetme.com but can't figure out how to do it. I can type in the message in the comment area but clicking the Send button doesn't do anything. What am I doing wrong? When I login and press the Login button the page does change and everything is fine. Anyone have any ideas or clues?

HtmlPage htmlPage = null;

HtmlElement htmlElement;

WebClient webClient = null;

HtmlButton htmlButton;

HtmlForm htmlForm;

try{

// Create and initialize WebClient object

webClient = new WebClient(BrowserVersion.FIREFOX_17 );

webClient.setCssEnabled(false);

webClient.setJavaScriptEnabled(false);

webClient.setThrowExceptionOnFailingStatusCode(false);

webClient.setThrowExceptionOnScriptError(false);

webClient.getOptions().setThrowExceptionOnScriptError(false);

webClient.getOptions().setUseInsecureSSL(true);

webClient.getCookieManager().setCookiesEnabled(true);

/*webClient.setRefreshHandler(new RefreshHandler() {

public void handleRefresh(Page page, URL url, int arg) throws IOException {

System.out.println("handleRefresh");

}

});*/

htmlPage = webClient.getPage("http://www.meetme.com");

htmlForm = htmlPage.getFirstByXPath("//form[@action='https://ssl.meetme.com/login']");

htmlForm.getInputByName("username").setValueAttribute("blah@gmail.com");

htmlForm.getInputByName("password").setValueAttribute("blah");

//Signing in

htmlButton = htmlForm.getElementById("login_form_submit");

htmlPage = (HtmlPage) htmlButton.click();

htmlPage = webClient.getPage("http://www.meetme.com/member/1234567890");

System.out.println("BEFORE CLICK");

System.out.println(htmlPage.asText());

//type message in text area

HtmlTextArea commentArea = (HtmlTextArea)htmlPage.getFirstByXPath("//textarea[@id='profileQMBody']");

commentArea.setText("Testing");

htmlButton = (HtmlButton) htmlPage.getHtmlElementById("profileQMSend");

htmlPage = (HtmlPage)htmlButton.click();

webClient.waitForBackgroundJavaScript(7000);

//The print is exactly the same as the BEFORE CLICK print

System.out.println("AFTER CLICK");

System.out.println(htmlPage.asText());

}catch(ElementNotFoundException e){

e.printStackTrace();

}catch(Exception e){

e.printStackTrace();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值