0

I have some code where clicking on a button will open the aspx page in a new tab. I'm able to download a valid pdf file by clicking on download icon in chrome viewer. The URL is dynamic and its query parameter changes with each login. Can someone help me on how to get the content of the file using selenium java? In network tab, the respons eis a html file with an embed and src is blank.

7
  • 1
    You should try using copyURLToFile: commons.apache.org/proper/commons-io/apidocs/org/apache/commons/… Then process the file. Right now it's hard to say, though, whether the URL is actually returning a file or another HTML page with an embed. (if it's an embed, get it's URL to process...) Commented Mar 24 at 19:45
  • The URL is not a direct link to PDF, instead its an aspx page generating pdf on the fly which is being viewed using chrome pdf viewer. I mean clicking on button in application opens a new tab with this aspx link. Upon inspecting, I'm seeing an embed tag with src as blank. So is there anyway I can download the pdf? would adding this option "plugins.always_open_pdf_externally", true" to chrome options work? are there any other ways to download pdf other than this? Commented Mar 25 at 16:28
  • in Chrome you can tell if the endpoint is returning a file, because it will generate a page with shadowroots, and the embed's src will say something like: "chrome-extension:..." In that case copyURLToFile should work on that URL. If the src is blank, JS must be making a call to get the file. Check network tab in Chrome's dev-tools. Commented Mar 25 at 16:37
  • Getting the same error. This is how I implemented it. log().info(url); URL pdfURL = new URL(url); File f = new File("path to some directory\\target123.pdf"); FileUtils.copyURLToFile(pdfURL, f); PDDocument pdf = PDDocument.load(f); Commented Mar 25 at 16:56
  • can you open the downloaded file? (in browser or with Acrobat?) If so, I'd go ahead and upgrade pdfBox, as it looks like they have a ton of updates in the new version. Commented Mar 25 at 21:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.