Skip to content
Advertisement

How to get the only PDF url from web page?

I am trying to get some DOM elements using Selenium and I am doing all of this using Java but I am getting this Error when trying it out:

JavaScript

I am still a newbie in all this but the code I am using to retrieve the DOM element is:

JavaScript

I believe the error is that it cannot find the XPath given although this xpath exists. Any help would be appreciated.

Thank you.

Advertisement

Answer

  • There is a href attribute is having pdf URL but that URL opens the pdf within webpage.

  • So I extracted the pdf URL from href attribute and fetched the pdf name from that then concatenated with https://www.qp.alberta.ca/documents/Acts/ URL.

You can write the code like below to get the pdf URL.

Code to get PDF URL:

JavaScript

Code to download PDF:

Required ChromOptions:

JavaScript

Accessing PDF:

JavaScript

OutPut:

JavaScript

Import for StringUtils:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement