Home > Back-end >  Selenium Firefox Python: Can we use relative path for downloads folder?
Selenium Firefox Python: Can we use relative path for downloads folder?

Time:09-09

Hello :) This is related to my question here Selenium Firefox Python: How to change download directory thank you so much @Thamognya for the assistance.

Can we use a relative path for this? We're using Docker in CircleCI and it will be much more convenient to use the relative path "ProjectTitle/downloads" as path for download. Currently it's working for absolute path /Users/this/is/the/absolute/path/downloads. Is there a way to format it to work for relative path?

I tried this but it goes to the default Downloads folder:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
    options = Options()
    options.set_preference("browser.download.folderList", 2)
    options.set_preference("browser.download.manager.showWhenStarting", False)
    options.set_preference("browser.download.dir", r"./downloads")
    options.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")
    context.driver = webdriver.Firefox(options=options)

Even for this it goes to the default Downloads folder as well:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
    options = Options()
    options.set_preference("browser.download.folderList", 2)
    options.set_preference("browser.download.manager.showWhenStarting", False)
    options.set_preference("browser.download.dir", r'ProjectTitle/downloads')
    options.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")
    context.driver = webdriver.Firefox(options=options)

Other attempts:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
    options = Options()
    options.set_preference("browser.download.folderList", 2)
    options.set_preference("browser.download.manager.showWhenStarting", False)
    options.set_preference("browser.download.dir", "ProjectTitle/downloads")
    options.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")
    context.driver = webdriver.Firefox(options=options)

Others:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
    options = Options()
    options.set_preference("browser.download.folderList", 2)
    options.set_preference("browser.download.manager.showWhenStarting", False)
    options.set_preference("browser.download.dir", r"./downloads")
    options.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")
    context.driver = webdriver.Firefox(options=options)

CodePudding user response:

You can always convert a relative path to an absolute path.

(https://stackoverflow.com/a/40311142/7058266)

Example:

>>> import os
>>> os.path.abspath("downloads_folder")
'/Users/michael/github/SeleniumBase/examples/downloads_folder'

Just convert the relative path to an absolute one before you use it.

  • Related