[英]Javascript HtmlUnit
I have a submit button that I can't click on..我有一个提交按钮,我无法点击..
<div class="button_green">
<a href="javascript:sendSMS();" title="Send SMS"><span>Send SMS</span></a>
</div>
I have tried this:我试过这个:
page = (HtmlPage) form.getInputByValue("Send SMS").click();
but that won't work.但这行不通。
Is there a way to click on this or run the Javascript with Java and HtmlUnit?有没有办法点击这个或使用 Java 和 HtmlUnit 运行 Javascript? And it is not my site, so can't do anything with the html-code..
而且它不是我的网站,所以不能对 html 代码做任何事情..
You can use XPath: 您可以使用XPath:
page = htmlPage.<HtmlDivision>getFirstByXPath("//div[@class='button_green'").click();
Which means: search for the first div
with a class
attribute of button_green
, and click it. 这意味着:使用
button_green
的class
属性搜索第一个div
并单击它。
W3schools has a good XPath tutorial . W3schools有一个很好的XPath教程 。
This should work. 这应该工作。
<script> function sendSMS(){ alert('your sms function work here~~'); } </script> <a href="#" onclick="javascript:sendSMS();" title="Send SMS"><span>Send SMS</span></a>
您将需要使用如下所示的button标记:
<button type="button">Send SMS</button>
If you cannot edit HTML then you can do this using jquery.如果您无法编辑 HTML,那么您可以使用 jquery 来完成此操作。 Try following code.
尝试以下代码。
$( ".button_green" ).click(function() { alert( "Handler for .click() called." ); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <div class="button_green"> <a href="javascript:sendSMS();" title="Send SMS"><span>Send SMS</span></a> </div>
To enable jquery first add a script line in the HTML page.要启用 jquery,首先在 HTML 页面中添加一个脚本行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.