更改现有网络驱动程序中的 ChromeOptions [英] Change ChromeOptions in an existing webdriver

查看:31
本文介绍了更改现有网络驱动程序中的 ChromeOptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:需要将文件从网络层次结构下载到同一层次结构下的本地驱动器.

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

示例 Web 层次结构:

Example Web Hierarchy:

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

下载文件 1 时,它应该存储在路径 1 - "C:....DownloadsParent 1Child 1"

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

下载文件 2 时,它应该存储在路径 2 - "C:....DownloadsParent 1Child 2"

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

问题:

当我在 chrome webdriver 中保留 "C:....DownloadsParent 1Child 1" 下载路径,同时在 setUp() & 中第一次初始化 webdriver 时下载文件 1",它会下载到预期的文件夹中.但是,当我在 chrome webdriver 中设置下一个C:....DownloadsParent 1Child 2"下载路径以在其中下载文件 2 时,它会打开另一个 chrome 浏览器,因为我正在使用另一个 webdriver 来设置路径 2.

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.

所需解决方案:

我想使用现有的 webdriver 来设置不同的 chrome 下载路径或您能想到的任何其他解决方法.

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

当前代码:

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.

提前致谢!

推荐答案

当您通过 ChromeOptions 配置 ChromeDriver 的实例以启动新的 Chrome 浏览器时 配置被烘焙chromedriver 可执行文件中,该可执行文件将在 WebDriver 的生命周期内持续存在并且保持不可编辑强>.

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.

即使您能够提取 ChromeDriverChromeSession 属性,例如会话 IDCookies 和来自发起的 浏览会话 的其他会话属性,您仍然无法更改 的这些属性ChromeDriver.

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.

更简洁的方法是在 tearDown(){} 方法中调用 driver.quit()关闭销毁 ChromeDriverChrome Browser 实例,然后跨越一组新的 ChromeDriver>Chrome 浏览器具有新配置集的实例.

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.

您可以在以下位置找到一些相关讨论:

You can find a couple of relevant discussions in:

这篇关于更改现有网络驱动程序中的 ChromeOptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆