Java 以编程方式单击网页按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16589417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically click a webpage button
提问by 1478963
How to programmatically click a webpage button in Java?
如何以编程方式单击 Java 中的网页按钮?
Given this button:
鉴于此按钮:
<div id="titlebutton" >Button</div>
In Javascript it would work like this:
在 Javascript 中,它会像这样工作:
var newWin = window.open(siteYouHaveAccessTo);
newWin.document.getElementById('titlebutton').click();
I was wondering how I would do this in Java, not Javascript (Is there a way of doing it with Java alone or do I have to import/use Javascript?).
我想知道如何在 Java 中而不是 Javascript 中执行此操作(有没有办法单独使用 Java 或我必须导入/使用 Javascript?)。
Edit:
编辑:
Clicking the Button will result in a call of a function called setText(). I am not sure how I would send a similar request as this function? Would it help if I pasted the function code?
单击按钮将导致调用名为 setText() 的函数。我不确定如何发送与此功能类似的请求?如果我粘贴功能代码会有帮助吗?
采纳答案by BaconCookies
You can't 'programmatically' click a button with Java alone, which is why we use JavaScript. If you want to get into controlling the browser such as clicking buttons and filling text fields you would have to use an automation tool. An automation tool that uses Java is Selenium. This is typically used as for test automation, but will do what you want it to do.
您不能单独使用 Java 以“编程方式”单击按钮,这就是我们使用 JavaScript 的原因。如果您想控制浏览器,例如单击按钮和填充文本字段,则必须使用自动化工具。使用 Java 的自动化工具是 Selenium。这通常用于测试自动化,但会做你想让它做的事情。
回答by Jeshurun
I'm not sure if this is what you are looking for, but if what you are looking to do is simply make a HTTP request in Java, this should do the trick:
我不确定这是否是您要找的,但如果您要做的只是在 Java 中发出 HTTP 请求,这应该可以解决问题:
HttpURLConnection urlConnection = null;
URL urlToRequest = new URL("http://yoursite.com/yourpage?key=val&key1=val1");
urlConnection = (HttpURLConnection) urlToRequest.openConnection();
urlConnection.setConnectTimeout(CONN_TIMEOUT);
urlConnection.setReadTimeout(READ_TIMEOUT);
// handle issues
int statusCode = urlConnection.getResponseCode();
if (statusCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
// handle unauthorized (if service requires user login)
} else if (statusCode != HttpURLConnection.HTTP_OK) {
// handle any other errors, like 404, 500,..
}
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
// Process response
Replace the timeout constants with your own values, and the URL to match that of your website. You can send any data you want to send with the click of the button as query parameters in the URL in the form of key/value pairs.
将超时常量替换为您自己的值,以及与您网站的 URL 匹配的 URL。您可以通过单击按钮以键/值对的形式发送任何要发送的数据作为 URL 中的查询参数。
回答by tgkprog
if you looking for browser automation can use selenium or java.awt.Robot
如果您正在寻找浏览器自动化可以使用 selenium 或 java.awt.Robot
Robot can send 'clicks' to the OS. have used it along with vbs scripts to first make sure a particular window has focus and then send text and clicks, and finally save results ... not very good as if the page changes things then you need to make adjustments. But the person I made this used it for 4 years.
机器人可以向操作系统发送“点击”。将它与 vbs 脚本一起使用,首先确保特定窗口具有焦点,然后发送文本和点击,最后保存结果……不太好,好像页面发生了变化,然后您需要进行调整。但是我做这个的人用了 4 年。