Python 2.7 使用Selenium/Python多次更改FirefoxProfile()首选项

Python 2.7 使用Selenium/Python多次更改FirefoxProfile()首选项,python-2.7,selenium-webdriver,Python 2.7,Selenium Webdriver,因此,我尝试根据使用Selenium的链接,将多个excel链接下载到不同的文件路径 我可以设置FirefoxProfile,将所有链接下载到某个单一路径,但我无法在尝试将不同文件下载到不同文件路径时动态更改路径。有人对此有办法吗 self.fp=webdriver.FirefoxProfile() self.ft.set_首选项(“browser.download.folderList”,2) self.ft.set_首选项(“浏览器.下载.启动时显示”,2) self.ft.set_首选项(

因此,我尝试根据使用Selenium的链接,将多个excel链接下载到不同的文件路径

我可以设置FirefoxProfile,将所有链接下载到某个单一路径,但我无法在尝试将不同文件下载到不同文件路径时动态更改路径。有人对此有办法吗

self.fp=webdriver.FirefoxProfile() self.ft.set_首选项(“browser.download.folderList”,2) self.ft.set_首选项(“浏览器.下载.启动时显示”,2) self.ft.set_首选项(“browser.download.dir”、“C:\SOURCE FILES\BACKHAUL”) self.ft.set_首选项(“browser.helperApps.neverAsk.saveToDisk”(“application/vnd.ms excel))

self.driver=webdriver.Firefox(Firefox\u profile=self.fp)


此代码将设置我想要的路径一次。但我希望能够在运行一个脚本时多次设置它。

您只能在初始化驱动程序时定义它。因此,要使用新路径执行此操作,您应该
驱动程序。退出
并重新启动它。

在Linux和Mac上,您可以设置配置文件首选项以下载到我需要的目录s是指向另一个目录的符号链接。然后,在运行Selenium驱动程序时,可以使用Python中的
os
库将符号链接的目标更改为要下载文件的目录

代码大纲(无try/exceptetc):


所以我以前尝试过,但我得到了错误:“进程无法访问该文件,因为它正被另一个进程使用:'c:\\users\\usrujy\\appdata\\local\\temp\\tmpckcz20\\cert8.db'。然后文件被下载到我的下载目录(不是我指定的)。
import os
from selenium import webdriver

DRIVER = "/usr/local/bin/geckodriver"

fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.dir", "/your/symlink/name")
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "your/mime/type")

driver = webdriver.Firefox(firefox_profile=fp, executable_path=DRIVER)

# delete symlink before changing to prevent a FileExistsError when changing the link
if os.path.exists("/your/symlink/name"):
    os.unlink("/your/symlink/name")
# actually change/create the link
os.symlink("/real/target/directory", "/your/symlink/name")
# download the file
driver.get("https://example.com/file")

# set new target directory
os.unlink("/your/symlink/name")
os.symlink("/different/target/directory", "/your/symlink/name")
driver.get("https://example.com/otherfile")