SXC88 SXC88 - 23 days ago 21
Python Question

Python selenium download files

I would like to use selenium in Python to automate the downloading process. But the current problem is that I can actually get to the right url (where the pdf file is located) using the xpath, but I cannot download the files because of the OS dial box. I found some solutions suggesting the use of

webdriver.FirefoxProfile().set.preference
. However, since I need to click on the website several times using selenium to get to the right page, I cannot set the url directly with
set.preference
at the beginning of the program. Could you help me to integrate the
set.preference
to my existing program? Thank you very much!!

PS. as you can see the website needs authentication.

Here is my current code:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import unittest
import os




class LoginTest(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Firefox()
self.driver.get("myinitialurl")

def test_Login(self):
driver=self.driver

emailFieldID="userNameInput"

passFieldID="passwordInput"
loginButtonID="submitButton"
BBButton="(//a[contains(@href,'blackboard')])"
coursebutton="(//a[contains(@href,'Course&id=_4572_1&url')])[1]"

docbutton="(//a[contains(@href,'content_id=_29867_1')])"
conbutton="(//a[contains(@href,'content_id=_29873_1')])"
paperbutton="(//a[contains(@href,'/xid-26243_1')])"

emailFieldElement=WebDriverWait(driver,10).until(lambda driver:driver.find_element_by_id(emailFieldID))

passFieldElement=WebDriverWait(driver,10).until(lambda driver:driver.find_element_by_id(passFieldID))
loginButtonElement=WebDriverWait(driver,10).until(lambda driver:driver.find_element_by_id(loginButtonID))

emailFieldElement.clear()
emailFieldElement.send_keys("username")
passFieldElement.clear()
passFieldElement.send_keys("password")
loginButtonElement.click()
BBElement=WebDriverWait(driver,50).until(lambda driver:driver.find_element_by_xpath(BBButton))
BBElement.click()
WebDriverWait(driver, 50).until(lambda driver: len(driver.window_handles) == 2)

window_after = driver.window_handles[1]
driver.switch_to.window(window_after)
courseElement=WebDriverWait(driver,50).until(lambda driver:driver.find_element_by_xpath(coursebutton))
courseElement.click()


After that normally I should open a pdf file on the website and a dial box. I would like to download the file.

the code of set.preference that I found is as follows:

fp = webdriver.FirefoxProfile()

fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir", os.getcwd())
fp.set_preferenc("browser.helperApps.neverAsk.saveToDisk", "application/pdf")

browser = webdriver.Firefox(firefox_profile=fp)
browser.get("url")
browser.find_element_by_partial_link_text("button").click()


So my question is how to integrate the second part in the first code so as to download the content triggered by the last click.

Otherwise, do you have other easier solutions??

Thank you very much!

Answer

Try to add two more preferences that you might need to download PDF file:

fp.set_preference("pdfjs.disabled", True)
fp.set_preference("plugin.disable_full_page_plugin_for_types", "application/pdf")
Comments