简体   繁体   English

如何使用Java下载链接到Javascript的文件

[英]How to download file linking to Javascript using Java

This is not a duplicate . 这不是重复项。 I have a Selenium solution for this, but I do not prefer using Selenium . 我有一个Selenium解决方案,但是我不喜欢使用Selenium。 I hope somevbody could help me with a HttpUrlConnection code that would deal with this javascript directly rather than finding a work around that pop up using Selenium 我希望有人能帮助我处理一个直接处理此javascript的HttpUrlConnection代码,而不是使用Selenium在弹出的窗口中找到解决方法


On a webpage I have the following link (covered with an image) 在网页上,我具有以下链接(包含图片)

     <a .............

     onclick="javascript:downLoad('CAMID(\xxx;)/folder[@name=\'test\']/folder[@name=\'test\']

    /reportView[@name=\'test\']/output[@name=\'test']',

    'spreadsheetML' )" alt="Download" ></a>

Ones I click on this link a pop up window shows up and I can click on the save 我单击此链接会弹出一个窗口,然后单击save

How can I get this file programmatically using Java? 如何使用Java以编程方式获取此文件? There must be a way to stream the file without even touching that pop up 必须有一种方法来流式传输文件,甚至不碰弹出窗口


So far I am using Selenium and Java Robot object to hit that save button . 到目前为止,我正在使用Selenium和Java Robot对象单击该save按钮。 But I am sure there must be a way to download it directly . 但是我确信必须有一种直接下载它的方法。

This question tells you how to find the popup window: Handling a popup window using selenium 这个问题告诉您如何查找弹出窗口: 使用硒处理弹出窗口

After that, you can use the usual Selenium API to click the save button. 之后,您可以使用常规的Selenium API单击保存按钮。

[EDIT] You can use HttpClient to download from a web server. [编辑]您可以使用HttpClient从Web服务器下载。

The first step would be to download the page with the link. 第一步是下载带有链接的页面。 Then, you can use various way to locate the link. 然后,您可以使用多种方法来找到链接。 You will then either have to execute the JavaScript or, if you're sure the script won't change often, replicate the code in Java to come up with the same URL. 然后,您将不得不执行JavaScript,或者,如果您确定脚本不会经常更改,则可以用Java复制代码以提供相同的URL。

Then, you can use HttpClient again to download the document directly. 然后,您可以再次使用HttpClient直接下载文档。

If you need to execute the JavaScript, you can use Rhino . 如果需要执行JavaScript,则可以使用Rhino If the script accesses various objects from the browser environment, look at envjs . 如果脚本从浏览器环境访问各种对象,请查看envjs

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

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