Python selenium download files -
i use selenium in python automate downloading process. current problem can right url (where pdf file located) using xpath, cannot download files because of os dial box. found solutions suggesting use of webdriver.firefoxprofile().set.preference
. however, since need click on website several times using selenium right page, cannot set url directly set.preference
@ beginning of program. me integrate set.preference
existing program? thank much!!
ps. can see website needs authentication.
here current code:
from selenium import webdriver 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 should open pdf file on website , dial box. download file.
the code of set.preference found 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 question how integrate second part in first code download content triggered last click.
otherwise, have other easier solutions??
thank much!
try add 2 more preferences might need download pdf
file:
fp.set_preference("pdfjs.disabled", true) fp.set_preference("plugin.disable_full_page_plugin_for_types", "application/pdf")
Comments
Post a Comment