6

Scenario: There is a requirement of downloading files from web hierarchy to local drive under same hierarchy.

Example Web Hierarchy:

Parent 1:  
  Child 1:  
    *File 1  
  Child 2:  
    *File 2  

When downloading File 1, it should store in path 1 - "C:\....\Downloads\Parent 1\Child 1\"

When downloading File 2, it should store in path 2 - "C:\....\Downloads\Parent 1\Child 2\"

Problem:

When I keep "C:....\Downloads\Parent 1\Child 1\" download path in chrome webdriver while initializing webdriver first time in setUp() & download "File 1", it downloads in expected folder. But when I set next "C:....\Downloads\Parent 1\Child 2\" download path in chrome webdriver for downloading File 2 in it, it opens another chrome browser because I am using another webdriver for setting path 2.

Required Solution:

I want to use existing webdriver to set different chrome download paths or any other workaround you can think of.

Current Code:

def setUp(self):  
browser = webdriver.Chrome(chromedriver_path, option_with_path_1_set)

def test_downloadFiles(self):  
\*code to download first file\*  
driver = webdriver.Chrome(chromedriver_path, option_with_path_2_set)  
\*code to download second file\*  

def tearDown(self):  
browser.quit()

Please let me know if you require any additional information.

Thanks in advance!

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • 1
    you can't change it, but you can download file to some temp location, and then move downloaded file to a different location, see solution to this question: https://stackoverflow.com/questions/22734399/python-selenium-webdriver-dynamically-change-download-directory – Kiril S. Oct 27 '18 at 18:37

1 Answers1

4

When you configure an instance of a ChromeDriver through ChromeOptions to initiate a new Chrome Browser the configuration gets baked into the chromedriver executable which will persist for the lifetime of the WebDriver and remain uneditable.

Even if you are able to extract the ChromeDriver and ChromeSession attributes e.g. Session ID, Cookies and other session attributes from the initiated Browsing Session still you won't be able to change those attributes of the ChromeDriver.

A cleaner way would be to call driver.quit() within tearDown(){} method to close and destroy the ChromeDriver and Chrome Browser instances gracefully and then span a new set of ChromeDriver and Chrome Browser instance with the new set of configurations.


tl; dr

You can find a couple of relevant discussions in:

DebanjanB
  • 118,661
  • 30
  • 168
  • 217