I'm on a PDF page in my browser and I'm trying to click on the download button on the top right corner. This picture is added for clarity. I've tried right clicking on the button, grabbing both the CSS selector and the XPath but in my code, it doesn't show up as an element. What am I doing wrong?
This is my code for reference.
page.goto(url);
page.click([link that opens up new tab]);
const [tab1, tab2, tab3] = await browser.pages();
await tab3.click("#download");
I managed to fix my problem by using the second solution posted here: How to download a pdf that opens in a new tab in puppeteer?. Basically, using puppeteer extra, I changed the behavior of clicking the link to trigger the download manually.
puppeteer.use(require('puppeteer-extra-plugin-user-preferences')({
headless: false,
timeout: 30000,
ignoreHTTPSErrors: true,
userPrefs: {
download: {
prompt_for_download: false,
open_pdf_in_system_reader: true
},
plugins: {
always_open_pdf_externally: true
}
}
}));
let browser = await puppeteer.launch();
let page = await browser.newPage();
...
await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: './'});
Thank you for the help!
You can use this code which is very easy:
from urllib import request
url = 'https://dagrs.berkeley.edu/sites/default/files/2020-01/sample.pdf'
request.urlretrieve(url, 'sample.pdf')