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
    评论
htmlunit-core-js 是一个基于 Rhino JavaScript 引擎的 JavaScript 引擎,它可以帮助你在 Java 应用程序中运行 JavaScript 代码。下面是一个简单的示例,演示如何使用 htmlunit-core-js 运行 JavaScript 代码: ```java import org.mozilla.javascript.Context; import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.ScriptableObject; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlPage; public class HtmlUnitCoreJsExample { public static void main(String[] args) throws Exception { // 创建一个 JavaScript 引擎上下文 Context context = Context.enter(); try { // 创建一个顶级作用域对象 Scriptable scope = context.initStandardObjects(); // 将一个 Java 对象添加到 JavaScript 环境中 Object wrappedOut = Context.javaToJS(System.out, scope); ScriptableObject.putProperty(scope, "out", wrappedOut); // 执行一段 JavaScript 代码 String script = "var message = 'Hello, world!'; out.println(message);"; context.evaluateString(scope, script, "example", 1, null); // 使用 HTMLUnit 来执行 JavaScript 代码 WebClient webClient = new WebClient(); HtmlPage page = webClient.getPage("https://www.baidu.com/"); String pageTitle = (String) context.evaluateString(scope, "document.title", "example", 1, null); System.out.println("Page title: " + pageTitle); } finally { // 释放 JavaScript 引擎上下文 Context.exit(); } } } ``` 这个示例包括以下步骤: 1. 创建一个 JavaScript 引擎上下文。 2. 创建一个顶级作用域对象,并将一个 Java 对象添加到 JavaScript 环境中。 3. 执行一段 JavaScript 代码,将输出打印到控制台。 4. 使用 HTMLUnit 库加载一个网页,并在 JavaScript 引擎环境中获取页面标题。 5. 释放 JavaScript 引擎上下文。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值