简体   繁体   English

单击带有HtmlUnit的javascript按钮

[英]Clicking a javascript button with HtmlUnit

I'm trying to click a Html button using HTMLUnit. 我正在尝试使用HTMLUnit单击HTML按钮。 Button code: 按钮代码:

<input class="Submit" value="I Accept." type="submit" name="ButtonSubmit" id="button" onclick="/*some looong js code*/" tabindex="16">

My code 我的密码

final HtmlSubmitInput baton = form.getInputByName("ButtonSubmit");
HtmlPage page2 = baton.click();

I tried using click(); 我尝试使用click(); method but it does not work. 方法,但不起作用。

also pressing enter key: 同时按回车键:

final HtmlTextInput textinput = form.getInputByName("SomeInput");
//filling input
HtmlPage page = textinput.type(KeyboardEvent.DOM_VK_ENTER);

I think problem is with the js code. 我认为问题在于js代码。 It just does't run using HTMLUnit. 它只是不使用HTMLUnit运行。 (Filling the form manually, with browers, works fine.) Thanks for help. (使用浏览器手动填写表格可以正常工作。)感谢您的帮助。

make sure to enable JS in your code 确保在代码中启用JS

webClient.getOptions().setJavaScriptEnabled(true); webClient.getOptions()setJavaScriptEnabled(真)。 webClient.getOptions().setThrowExceptionOnScriptError(false); webClient.getOptions()。setThrowExceptionOnScriptError(false);

I don't know if this will solve your problem, but there are two things I do differently to get around this (Javascript doesn't seem to click on elements): 我不知道这是否可以解决您的问题,但是为了解决这个问题,我做了两件事有不同的处理(JavaScript似乎没有点击元素):

  • Use form.getElementById("button") instead of getInputByName 使用form.getElementById("button")代替getInputByName
  • Use Thread.sleep to wait (around 350 ms seems to be the magic number) for the effect to take place 使用Thread.sleep等待(大约350毫秒似乎是魔幻数字)效果发生

Even with this, I still see non-determinstic test failures because the effects of the click sometimes just don't seem to happen. 即使这样,我仍然会看到不确定的测试失败,因为单击的影响有时似乎没有发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM