Network & File Transfers

Upload

Clicks the matched element and gives the file to the input behind it. Mandatory parameter: elementId. Optional: css, gpalElements, modifiers, uploadPath for one file, and uploadPaths for several. One of css, elementId, gpalElements, or modifiers must be present. The request goes out as multipart, which is why this route is the only one that does not send its parameters as plain JSON. How the file reaches the input differs per engine and the fluent call picks the path for you, so the engine samples below are what each one does underneath rather than four ways of asking.

NOTE

Attaching more than one file: build a GPALFile with a WithFileName call per file and pass the one object. Selenium sends the whole set in a single call, paths separated by newlines, but only when the input carries the multiple property, because chromedriver adds a file on every send and geckodriver throws on the second. Puppeteer sends uploadPaths instead of uploadPath once there is more than one. OttoMagic sends one file per call and keeps the files already on the input, so several calls build the set up rather than replace it.

WARNING

Hardware input needs a window. The hardware path types into the dialog the browser opens, and a headless browser opens no dialog, so asking for hardware input on a headless run leaves the file unattached and the workflow carrying on as though it worked. The dialog is also named differently per browser, Open on Chrome and Edge and File Upload on Firefox, so a browser GPAL does not recognise finds no window and the upload is reported as a failure.

Examples

GPAL Fluent: High-level fluent C# API

//This is the call to reach for. It reads the GPALFile, picks the engine path, and publishes an INFO event naming each file as it goes. A second WithFileName adds a file rather than replacing the first. Hardware input is the one path that opens the real file dialog, and it is chosen by the selector or by the browser settings rather than by this call.

GPALFile uploadFiles = GPAL.File

.WithFileName(@"C:invoicesmarch.pdf")

.WithFileName(@"C:invoicesapril.pdf")

.ToGPALObject();


browser

.WithSelector(uploadButton)

.LeftClickAndUpload(uploadFiles);

MagicHelper: MagicHelper utility library

//Two overloads over the same route, one taking a path and one a GPALFile, which pick WithUploadFile or WithUploadFiles for you. The element is named by its Css, which is what an OttoMagic element id is. OttoMagic cannot read your disk from the page, so the native app reads the file and sends the bytes with its name and type; the extension rebuilds it in the page, adds it to the ones already on the input, and raises change and input. A large file makes that whole journey in memory.

browser.MagicHelper.LeftClickAndUpload("upload123", @"C:invoicesmarch.pdf");


// or hand it a GPALFile

browser.MagicHelper.LeftClickAndUpload("upload123", uploadFiles);

Puppeteer Client: Puppeteer-style C# client

//LeftClickAndUpload takes a GPALElement, a list of them, or an element id. UploadFrom reads the GPALFile and sends uploadPath for one file or uploadPaths for more than one, which Puppeteer sets on the input over CDP without a dialog.

browser.PuppeteerClient

.LeftClickAndUpload(elementHandle)

.UploadFrom(uploadFiles)

.Execute();

Puppeteer Communicator: Low-level communicator

//The file argument is typed as object and takes a single path or any list of them, and the paths are made absolute before they are sent. The modifiers argument is carried for consistency with the other click routes and is not read here.

await browser.PuppeteerCommunicator

.LeftClickAndUpload(elems, @"C:invoicesmarch.pdf", modifiers, sessionId, responses);


// a list of paths attaches all of them

await browser.PuppeteerCommunicator

.LeftClickAndUpload(elems, paths, modifiers, sessionId, responses);

REST Client: Direct REST/HTTP client

//WithUploadFile takes a single path and WithUploadFiles takes a GPALFile. Both force the request to POST, since the file travels in the body rather than in the parameters.

browser.OttoMagicClient

.LeftClickAndUpload("upload123")

.WithUploadFile(@"C:invoicesmarch.pdf")

.Execute();


// several files, from a GPALFile

browser.OttoMagicClient

.LeftClickAndUpload("upload123")

.WithUploadFiles(uploadFiles)

.Execute();

💬 Ask GPAL