简体   繁体   中英

How to update firefox driver options in Selenium Python

so I'm trying to make a web crawler to download stuff online and in my class method i have

Class Webcrawler():
    def __init__(self, file):
        self.file = file
        self.folderName = None
        self.directory = 'C:\\Downloads\\'
        self.options = Options()
        self.options.set_preference('browser.download.folderList', 2) 
        self.options.set_preference("browser.download.dir", self.directory + '\\' + self.folderName)
        self.driver = webdriver.Firefox(options=self.options)

And I have a createDir function, which creates the folder where the files will be downloaded and updates self.folderName, but the driver never seems to update its options at all. How can I force it to update download.dir directory?

As and alternative you can use the following code block which will create a new folder on each execution as per the current timestamp as follows:

Class Webcrawler():
    def __init__(self, file):
    self.directory = 'C:\\Downloads\\'
    self.fmt='{self.directory}%Y-%m-%d-%H-%M-%S'        
    self.options = Options()
    self.options.set_preference('browser.download.folderList', 2) 
    self.options.set_preference("browser.download.dir", os.makedirs(datetime.datetime.now().strftime(self.fmt).format(directory=self.directory)))
    self.driver = webdriver.Firefox(options=self.options)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM