Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

python - 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:....DownloadsParent 1Child 1"

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

Problem:

When I keep "C:....DownloadsParent 1Child 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:....DownloadsParent 1Child 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!

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...