I made a python script that would log into a website with selenium and download data off a certain page into a directory the script created based on what it downloaded. The problem I run into however, is that if I want to download from multiple pages into multiple directories, I need to remake the webdriver.Chrome with a different default download directory, this makes the script have to re-login another time. This is unsustainable since the site I am using has only so many logins per day for security measures so it could not work long term. Is there any way to change the directory of a selenium instance of chrome while the instance is still running? My original code is posted below:
#Initialize the download directory folder
root = tkinter.Tk()
root.update()
path = filedialog.askdirectory(title="Choose a location to save the files")
root.destroy()
#make the download path
Dpath = path + '/' + ClientCode
Dpath = Dpath.replace('/','\\')
create the new folder
os.mkdir(Dpath)
#Creates Chrome Instance to log into portal
options = webdriver.ChromeOptions()
options.add_argument('--lang=EN')
#Configures download folder
preferences = {"download.default_directory": "%s" % Dpath }
options.add_experimental_option("prefs", preferences)
#options.add_argument("--headless") #<- if you want to run in background mode
driver = webdriver.Chrome(executable_path='chromedriver', chrome_options=options)
driver.delete_all_cookies()
driver.get(url)
driver.find_element_by_xpath("//*[@id='formuserinput']").send_keys(login)
driver.find_element_by_xpath("//*[@id='formpassinput']").send_keys(password+'\n')
#Clicks link on page to download file
driver.find_element_by_xpath(//html/body/div[1]/div[2]/div[2]/div[3]).click()