3

The ChromeDriver for Selenium was able to open an embedded PDF after login, but how can I save the PDF file in chrome to local disk? thanks.

3
  • 3
    Welcome to Stackoverflow! You're likely to get a better response to your question if you post some code that shows what you've tried to solve the problem yourself. Commented Oct 28, 2015 at 22:21
  • I managed to achieve the task by disable chrome pdf viewer and let the chrome browser directly download the pdf file Commented Oct 30, 2015 at 14:09
  • Possible duplicate of Chromedriver, Selenium - Automate downloads Commented May 7, 2018 at 9:52

1 Answer 1

10
def download_pdf(lnk):
    options = webdriver.ChromeOptions()
    tgt = tempfile.mkdtemp()
    profile = {"plugins.plugins_list": [{"enabled":False,"name":"Chrome PDF Viewer"}],
        "download.default_directory" : tgt}
    options.add_experimental_option("prefs",profile)
    driver = webdriver.Chrome(CHROMEDRIVER, chrome_options = options)
    driver.get(lnk)
    driver.find_element_by_id('userName1').send_keys('username')
    driver.find_element_by_id('password1').send_keys('password')
    driver.find_element_by_id('loginButton1').click()

    ftgt = os.path.join(tgt,'downloaed.pdf')
    while not os.path.exists(ftgt):
        time.sleep(3)
    driver.close()
    return ftgt
Sign up to request clarification or add additional context in comments.

1 Comment

Why is it necessary to use tempfile? And why to wait for os.path.exists?

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.