简体   繁体   English

如何使用 java Selenium WebDriver 下载文件?

[英]How do I download a file using java Selenium WebDriver?

I'm using Selenium 2.21.0 with Java 6. How do I use the Selenium WebDriver API to download a file on a web page?我在 Java 6 中使用 Selenium 2.21.0。如何使用 Selenium WebDriver API 下载网页上的文件? That is, there is a link that causes the download of an Excel file to start.也就是说,有一个链接会导致开始下载 Excel 文件。 I would like to know how to initiate that download, determine when its finished, and then figure out where the file got downloaded to on my local system.我想知道如何启动该下载,确定它何时完成,然后找出文件在本地系统上的下载位置。

Once you click on any link to download a file, it depends on browsers behaviour like Chrome Behaviour: It will start downloading the file by default, as soon as the user click on the link of any file.单击任何链接下载文件后,它取决于浏览器行为,如 Chrome 行为:默认情况下,只要用户单击任何文件的链接,它就会开始下载文件。 IE Behaviour: IE displays a bar at the bottom of the window and displays the option to Save or Cancel the file download. IE 行为:IE 在窗口底部显示一个栏,并显示保存或取消文件下载的选项。 FireFox Behaviour: This will display an dialog box window and displays the option to Save or Cancel the file download. FireFox 行为:这将显示一个对话框窗口并显示保存或取消文件下载的选项。 So this can be achieved with the help of FireFox Profile.所以这可以在 FireFox Profile 的帮助下实现。 And Before downloading any file you have to pass the MIME type of the file to FireFox Profile.并且在下载任何文件之前,您必须将文件的 MIME 类型传递给 FireFox 配置文件。 Some of the commonly used MIME types are: Text File (.txt) – text/plain PDF File (.pdf) – application/pdf CSV File (.csv) – text/csv MS Excel File (.xlsx) – application/vnd.openxmlformats-officedocument.spreadsheetml.sheet MS word File (.docx) – application/vnd.openxmlformats-officedocument.wordprocessingml.document一些常用的 MIME 类型是: 文本文件 (.txt) – 文本/纯 PDF 文件 (.pdf) – 应用程序/pdf CSV 文件 (.csv) – 文本/csv MS Excel 文件 (.xlsx) – 应用程序/vnd .openxmlformats-officedocument.spreadsheetml.sheet MS word 文件 (.docx) – application/vnd.openxmlformats-officedocument.wordprocessingml.document

HERE IS THE CODE:这是代码:

    import org.openqa.selenium.By;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxProfile;

    public class DownloadFiles {

    public static void main(String[] args) throws InterruptedException {
    //Create FireFox Profile object
    FirefoxProfile p = new FirefoxProfile();

    //Set Location to store files after downloading.
    profile.setPreference("browser.download.folderList", 2);

   //Set preference not to file confirmation dialogue
    profile.setPreference("browser.helperApps.neverAsk.saveToDisk", 
        "application/vnd.openxmlformats-
        officedocument.spreadsheetml.sheet"); 

  // Specify the local system path where to download
     p.setPreference("browser.download.dir", "D:\\downloads");
  // Pass Profile parameters in Firefox browser
    FirefoxDriver driver = new FirefoxDriver(profile);  

    // Open APP to download application
    driver.get("http://url");

    // Click to download 
    driver.findElement(By.xpath("//html[@attribute='value']")).click();

    Thread.sleep(5000);

    driver.close();

Hope it will solve your queries.希望它能解决您的疑问。 Happy coding.快乐编码。

I am specifically focusing of the Firefox browser you can use where the download option comes with pop up option when any download link is been click by the visitor.我特别关注您可以使用的 Firefox 浏览器,当访问者单击任何下载链接时,下载选项带有弹出选项。 It is shows two buttons and two radio button that gives us the option of save and open the file directly without downloading it afterwards if we find the file is useful we can download it explicitly though the download icon on the above toolbar of the Firefox browser.它显示了两个按钮和两个单选按钮,让我们可以选择直接保存和打开文件,而无需在之后下载它,如果我们发现文件有用,我们可以通过 Firefox 浏览器上面工具栏上的下载图标明确下载它。 So you can perform the following steps所以你可以执行以下步骤

1) Clicking on the download link 1)点击下载链接

WebDriver driver = new FirefoxDriver();

driver.findElement(By.linkText(“somelink”)).click();

The above code can help the WebDriver to identify the object and perform the action which is mentioned上面的代码可以帮助WebDriver识别对象并执行上面提到的动作

2) Once the download link is clicked the firefox browser will pop up a download dialog box with multiple option Like Save radio button, open radio button, ok button, cancel button inorder to use this You can use the Robot class or the Keys in the WebDriver Like 2) 点击下载链接后,firefox 浏览器会弹出一个下载对话框,里面有多个选项 像保存单选按钮,打开单选按钮,确定按钮,取消按钮才能使用这个你可以使用 Robot 类或者里面的 Keys网络驱动喜欢

Robot r = new Robot();


r.KeyPress(KeyEvent.VK_TAB); 

you can use as many time the above code to press the tab button您可以多次使用上面的代码来按下选项卡按钮

r.KeyRelease(KeyEvent.VK_TAB);

you have to release the pressed key你必须松开按键

lastly perform the enter最后执行输入

r.KeyPress(KeyEvent.VK_ENTER);

That's it it will help you to download the object when the download link is pressed就是这样,它会帮助您在按下下载链接时下载对象

Hope it will help you希望它会帮助你

暂无
暂无

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

相关问题 Java-Selenium WebDriver-如何检查“文件下载”对话框是否可见? (不下载仅能见度) - Java - Selenium WebDriver - How do I check if the File Download Dialog Box is visible? (Not download Only visibility) 如何使用与wget集成的Selenium WebDriver(Java)下载文件 - How to download a file using Selenium WebDriver (Java) integrated with wget 如何在Java中使用Selenium webdriver下载.docx文件? - How to download .docx file using Selenium webdriver in Java? 如何在JAVA中使用Selenium在firefox Webdriver上接受下载提示? - How do I accept the download prompt on the firefox webdriver using selenium in JAVA? 如何使用带有Java的Selenium WebDriver在iframe中拖动对象 - How do I drag an object in an iframe using Selenium WebDriver with Java 如何使用 Selenium webdriver 和 Java 为 firefox 设置代理? - How do I set a proxy for firefox using Selenium webdriver with Java? 如何使用 Java 设置 Selenium WebDriver? - How do I setup Selenium WebDriver with Java? 如何在Java中使用Selenium WebDriver上传文件 - How to upload file using Selenium WebDriver in Java 如何使用浏览器堆栈或GRID上的Selenium Webdriver(JAVA)下载文件并检查其内容? - How to download a file using Selenium Webdriver(JAVA) on the browserstack or GRID and check its Content? 如何在Java中使用Selenium Webdriver计算下载文件的大小,状态,剩余时间和传输率 - How to calculate download file Size, Status, Time left and Transfer rate using Selenium Webdriver in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM