Tutorials

Tutorials

Downloading Files with a Right-Click

Some links only offer a file when you right-click and choose Save Link As, rather than a normal left-click download. RightClickAndDownload drives that context-menu flow for you, and GPALFile.ReturnFilenames tells you exactly what landed on disk and where.

Complete Program

Here's the whole workflow, start to finish. Each piece is broken down and explained below.

using GenerallyPositive;

using GenerallyPositive.Browser;

using static GenerallyPositive.Enums;

using System.Diagnostics;

GPAL

.WithPublishToConsole()

.WithDriverLocation(@"C:drivers")

.WithUseOttoMagic(@"C:OttoMagic");

Selector linkSelector = GPAL.Selector

.WithXPath("//a[normalize-space()='How to Download files in Selenium Webdriver']")

.WithSelectorName("Download Link")

.WithOffsetY(5)

.ToGPALObject();

GPALFile fileList = @"c:sdi est.html";

IBrowser browser = GPAL.Browser

.WithBrowserType(BrowserType.Chrome)

.WithUseAutomationEngine(AutomationEngine.OttoMagic)

.WithDownloadLocation(@"c:downloads")

.WithDriverLocation(@"c:drivers")

.WithOverwriteExistingFile(false)

.WithUseDirectDownload(true)

.ToGPALObject();

browser.GoTo("https://sqa.stackexchange.com/questions/2197/how-to-download-a-file-using-seleniums-webdriver");

browser

.WithSelector(linkSelector);

browser

.MoveTo();

browser

.RightClickAndDownload(fileList)

.Close(true);

Debug.WriteLine($"File saved to {fileList.ReturnFilenames[fileList.Filenames.Count - 1]}.");

Configuring Where Downloads Land

WithDownloadLocation tells the browser which folder to save into. WithUseDirectDownload skips any save dialog and writes straight to disk, and WithOverwriteExistingFile(false) means a download that would collide with an existing filename is renamed instead of clobbering it.

IBrowser browser = GPAL.Browser

.WithBrowserType(BrowserType.Chrome)

.WithUseAutomationEngine(AutomationEngine.OttoMagic)

.WithDownloadLocation(@"c:downloads")

.WithDriverLocation(@"c:drivers")

.WithOverwriteExistingFile(false)

.WithUseDirectDownload(true)

.ToGPALObject();

Aiming the Context Menu with WithOffsetY

WithSelector marks the link as the target for the next actions, and MoveTo moves the mouse there - which a real browser needs before a right-click context menu makes sense. WithOffsetY(5) nudges the click point a few pixels down from the element's center, which can matter when a link sits flush against neighboring text or another control.

Selector linkSelector = GPAL.Selector

.WithXPath("//a[normalize-space()='How to Download files in Selenium Webdriver']")

.WithSelectorName("Download Link")

.WithOffsetY(5)

.ToGPALObject();

browser

.WithSelector(linkSelector);

browser

.MoveTo();

TIP

WithSelectorName doesn't change how the selector matches - it just gives the selector a friendly name that shows up in GPAL's event and exception messages, making it much easier to tell which element a failure was about.

RightClickAndDownload and GPALFile

RightClickAndDownload opens the browser's context menu on the selected element, chooses the save/download option, and waits for the resulting file to appear in the configured download location. The GPALFile you pass in - fileList - is updated with the new file's path once the download completes.

GPALFile fileList = @"c:sdi est.html";

browser

.RightClickAndDownload(fileList)

.Close(true);

WARNING

As with other workflows, Close(true) shuts down the browser and kills the driver process. Read anything you need from fileList before or immediately after this call, not after the driver is gone.

Reading Back the Saved Path with ReturnFilenames

fileList.Filenames is the list of names GPALFile knows about, and fileList.ReturnFilenames returns the actual paths on disk for those names - including any renaming that happened because of WithOverwriteExistingFile(false). Indexing the last entry gives you the file that was just downloaded.

Debug.WriteLine($"File saved to {fileList.ReturnFilenames[fileList.Filenames.Count - 1]}.");