Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change ChromeOptions in an existing webdriver

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!

like image 258
Siddharth Kulkarni Avatar asked Oct 27 '18 16:10

Siddharth Kulkarni


1 Answers

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:

  • How to set selenium webdriver from headless mode to normal mode within the same session?
  • How do I make Chrome Headless after I login manually
like image 52
undetected Selenium Avatar answered Nov 09 '22 03:11

undetected Selenium