I've been attempting to download an embedded PDF from a webpage using Protractor selenium. However, I seem to be stuck when it comes to actually downloading the file as I always encounter the following error:
- Failed: No element found using locator: By(css selector, *[id="download"])
Even after switching to the iframe, the button cannot be located.
I have also tried the solution mentioned in this answer, where it extracts the src attribute value and directly goes to the URL, but the same issue persists. The download button (icon) remains elusive.
We both have identical requirements, simply needing to click the download icon embedded within the PDF inside an iframe. An example page can be seen here.
Below is the code snippet I am currently using:
const iframe = $('#printFrame'),
downloadBtn = $('#download'),
content = $('#content');
await this.disableWaitForAngular();
await browser.wait(EC.visibilityOf(iframe), waitTimeout);
console.log("Switching to iframe...");
await browser.switchTo().frame(iframe.getWebElement());
await browser.wait(EC.visibilityOf(content), waitTimeout);
await browser.actions().mouseMove(content).perform();
console.log("Waiting for download button.");
await browser.wait(EC.visibilityOf(downloadBtn), waitTimeout);
await downloadBtn.click();
await browser.switchTo().defaultContent();
await this.enableWaitForAngular();
UPDATE:
I attempted to inject the following code as suggested in one of the previous answers before and after switching frames, but it resulted in an error.
const downloadIcon: WebElement = await browser.executeScript('return document.querySelector("#viewer").shadowRoot.querySelector("#toolbar").shadowRoot.querySelector("#downloads").shadowRoot.querySelector("#download").shadowRoot.querySelector("#icon > iron-icon");');
await downloadIcon.click();
Error:
- Failed: javascript error: Cannot read property 'shadowRoot' of null
(Session info: chrome=87.0.4280.66)
(Driver info: chromedriver=87.0.4280.20 (c99e81631faa0b2a448e658c0dbd8311fb04ddbd-refs/branch-heads/4280@{#355}), platform=Windows NT 10.0.14393 x86_64)
Reference image of the download icon: https://i.sstatic.net/8Zd7g.png