I am trying to download a PDF with Chromium and Selenium in Python. I am able to download it, if I download it as a file, but is it possible to get it as a string, like I would if it was downloaded with requests? Eg import requests; requests.get(url).content?
My current code is
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.africau.edu/images/default/sample.pdf")
This opens the pdf, but does not download. I can also do
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option(
"prefs",
{
"download.default_directory": "/home/username/Downloads/", # Change default directory for downloads
"download.prompt_for_download": False, # To auto download the file
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True, # It will not show PDF directly in chrome
},
)
options.add_argument("--headless")
driver = webdriver.Chrome(options=options)
driver.get("http://www.africau.edu/images/default/sample.pdf")
This works, but it downloads a PDF-file to my downloads folder, which means I would need to read it to get the PDF in code. How can I get the pdf without needing to download it as a file first?
