afish
afish

Reputation: 39

webdriverio - download pdfs in chrome

I am new to using webdriverio, and attempting to automatically download a pdf file. The file opens in the browser and I cannot figure out how to download it - ideally to a folder specified on my local machine. I see some old forum posts which possibly suggest using chromedriver, however, due to minimal code snippets provided, I cannot understand if it's the correct approach though. Here is what I have this far (although the last two lines do not work):

const LoginPage = require('../pageobjects/login.page');

describe('Payroll Download Application', () => {
    it('Login Fail Page', async () => {

        await LoginPage.open();
        await LoginPage.login();
        await $("a[href='PayCycleReports']").click()
        await $('a=Payroll Summary').click()
        const handles = await browser.getWindowHandles()
        await browser.switchToWindow(handles[1])
        const elem = await $("#viewer").shadow$("#toolbar").shadow$("#downloads").shadow$("#downloads").shadow$("#download")
        await elem.click()
    });
});

Any help to figure it out would be greatly appreciated. Thanks :)

Upvotes: 0

Views: 991

Answers (1)

This can be done with browser.execute. It will be necessary to select the element through JS and add the 'download' attribute to it. If you click on an element with the 'download' attribute, the pdf will not open but will be downloaded. Example:

await browser.execute(function(){
        document
        .querySelector("#node-32 > div > div > div > ul:nth-child(4) > li:nth-child(1) > a")
        .setAttribute("download", "download")
    },
);

If you need a different selector (not CSS) you can use getElementById , getElementsByName, getElementsByTagName, getElementsByClassName, getElementByXPathand others.

Upvotes: 0

Related Questions