如何在 Java 应用程序中打开网页并运行我自己的 javascript 代码
我想打开一个网页并从 java 应用程序中运行 javascript 代码。 例如,我想打开页面 www.mytestpage.com 并运行以下 javascript 代码:
document.getElementById("txtEmail").value="[email protected]";
submit();
void(0);
这在浏览器中有效...我如何在 java 应用程序中以编程方式执行此操作?
谢谢!
I would like to open a webpage and run a javascript code from within a java app.
For example I would like to open the page www.mytestpage.com and run the following javascript code:
document.getElementById("txtEmail").value="[email protected]";
submit();
void(0);
This works in a browser...how can I do it programatically within a java app?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 Rhino 来执行 JavaScript,但您不会有可用的 DOM - 即
document.getElementById()
可以工作。您可以使用 HTML 单元(无头)或 WebDriver/Selenium(驱动浏览器)在具有可用 DOM 的环境中执行 JavaScript。
You can use Rhino to execute JavaScript but you won't have a DOM available - i.e.
document.getElementById()
would work.You can use HTML Unit (headless) or WebDriver/Selenium (Driving a browser) to execute JavaScript in an environment that has a DOM available.
我不确定您在寻找什么,但我假设您想编写自动 POST 请求。这可以通过 Http 客户端库 来完成。只需设置适当的请求(POST 或 GET)参数即可。
查看 示例 - 使用此库,您可以也进行基本身份验证或发布文件。
I'm not sure what you are looking for but I assume that you want to write automated POST request. This can be done in with Http Client library. Only you have to set appropriate request (POST or GET) parameters.
Look at examples - with this library you can do basic authentication or post files too.
你的问题有点含糊,因为我们不知道Java程序的位置。
如果这是您页面内的 Java 小程序,您应该查看 Java<->JavaScript 交互,它运行良好。
如果您需要一个单独的 Java 程序来控制浏览器,例如在地址栏中发送小书签(如您的标签之一所示),则有点困难(取决于目标浏览器),也许看看 Robot 类。
Your question is a bit ambiguous, as we don't know the position of the Java program.
If that's a Java applet inside your page, you should look at Java<->JavaScript interaction, it works well.
If you need a separate Java program to control a browser, like sending a bookmarklet in the address bar (as one of your tags suggests), it is a bit harder (depends on target browser), perhaps look at the Robot class.
有一个用 Java 编写的 Rhino JS 引擎,您可以在 Tomcat 等应用服务器上运行并向其提供 JS但是 - 目前还不清楚你想用这个做什么?
还有Envjs模拟浏览器环境,它基于Rhino,但足够完整运行jQuery 和/或原型
There's Rhino JS engine written in Java that you can run on app server such as Tomcat and feed JS to, however - it's not clear what are you trying to do with this?
There's also Envjs simulated browser environment which is based on Rhino but complete enough to run jQuery and/or Prototype
DWR(和其他框架)现在支持“反向 ajax”。一般的想法是,您使用三种方法之一与客户端进行通信:
无论哪种方法(这通常是配置时决定而不是编码问题),您将可以完全访问您想要进行的任何/所有 js 调用。
查看DWR 的参考页面以获得很好的解释。
DWR (and other frameworks) now support "reverse ajax." The general idea is that you use one of three methods to communicate back to the client:
Regardless of method (which is typically a configuration-time decision and not a coding issue), you will have full access to any/all js calls you want to make.
Check out the reference page from DWR to get a pretty good explanation.