2

I am unable to download a CSV file using Selenium-Webdriver in a Chrome Browser window (Version 73.0.3683.86 (Official Build) (64-bit)) that opens when doing automated tests. I am using Selenium version: 3.4

Here is the HTML:

<h4>
<a href="/Hardware/ExportCsv?hardId=USB" download="Drives.csv" id="excel-btn">
        <img src="../../Content/Images/excel.svg">
        Export csv file.
    </a>
</h4>

Neither click() nor using the Actions class work. If I click the link with my mouse, the file is downloaded.

Browser.driver.findElement(By.id("excel-btn")).click();
action.moveToElement(Browser.driver.findElement(By.id("excel-btn"))).click().perform();

I expect the file to be downloaded.

Instead, however, the download link text is just being highlighted.

Important to note: in contrast, I can find other elements, assign them to a WebElement and execute Selenium actions on those elements successfully. It is just the download that is eluding me.

  • Can't help much without providing actual HTML of the page you are automating,meanwhile I could recommend to try clicking element by executing java script: public void clickWithJavaScript(WebElement element) { JavascriptExecutor executor = (JavascriptExecutor) driver; executor.executeScript("arguments[0].click();", element); } – Matthewek Apr 03 '19 at 12:37
  • Thanks @Matthewek I tried the JavascriptExecutor as well--did not work. Should have added that attempt as well. – WindSurfDude Apr 03 '19 at 12:44
  • Possibly your browser settings are different from the settings of the browser run by selenium. Anyway, you need to provide more relevant information, if you want us to help: browser type and version, HTML code, Selenium version, ... – Würgspaß Apr 03 '19 at 12:58
  • @Würgspaß, edited the post to reflect your points. Thank you. – WindSurfDude Apr 03 '19 at 14:23
  • @WindSurfDude Upvoted your question. Sadly, I am not into Chrome. So I cant help you, but some other one might. – Würgspaß Apr 03 '19 at 17:28

1 Answers1

1

I found an answer that works! The same solution that allows you to download files in a headless browser applies in this case as well. Essentially, there is a feature to prevent software from downloading files to your computer via the Chrome Browser. To get around that, you need to .setDownloadBehavior to allow downloading. (Make sure to set the Download Path to your download directory.)

Read more about it here; and here for code samples in Java, Python and C#.