下载文档Selenium和Firefox

时间:2018-08-30 10:54:32

标签: python python-2.7 selenium firefox download

我刚刚编写了以下代码,用于将文档下载到特定的文件夹,但是它始终将文件下载到默认文件夹中。Downloads,这是什么问题? 感谢帮助

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver import ActionChains
from selenium.webdriver.common.action_chains import ActionChains
import time
import os

options = Options();
options.set_preference("browser.download.dir","/Desktop/test-download");
options.set_preference("browser.download.folderList",2);
options.set_preference("browser.download.manager.showWhenStarting", False);
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms- 
powerpoint");
driver = webdriver.Firefox(firefox_options=options);

driver.get("https://legacy.python.org/doc/essays/ppt/lwnyc2002/intro22.ppt") 
driver.find_element_by_xpath("**xpath of document**").click()

1 个答案:

答案 0 :(得分:0)

您将必须使用FirefoxProfile来执行此操作(缩进可能会损坏):

fprofile = webdriver.FirefoxProfile()
fprofile.set_preference("browser.download.dir", "/Desktop/test-download")
fprofile.set_preference("browser.download.folderList", 2)
fprofile.set_preference("browser.download.manager.showWhenStarting", False)
fprofile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms- 
powerpoint")
driver = webdriver.Firefox(firefox_profile=profile)