Unable to access the modal elements to download pdf with selenium

574 Views Asked by At

I am web scraping a website that opens up the pdf I want to access in a modal view. I want to download/access this pdf.

It opens up as a pdf viewer in a modal and I can't access the download button or any other element through the Xpath. On accessing any element through Xpath, it returns an empty list .

The modal dialog box that needs to be accessed

The inspect section of the modal view

1

There are 1 best solutions below

1
On

I noticed that your screenshot about The inspect section of the modal view .

The element has an attribute original_url which is the original url of the pdf file.

And reference to this answer, you can try to directly download the pdf file by configurating webdriver.ChromeOptions().

So in your case it can be like this:

from selenium import webdriver

profile = {
    'download.prompt_for_download': False,
    'download.default_directory': '/path/to/download/the/pdf',
    'download.directory_upgrade': True,
    'plugins.always_open_pdf_externally': True,
}
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', profile)
driver = webdriver.Chrome(options=options)
driver.get('your_url')

# Your code to handle the Captcha

# When you open the modal dialog box
pdf_url = driver.find_element("id", "plugin").get_attribute("original_url")
driver.get(pdf_url)

# Chrome will download the PDF automatically