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

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

问题描述

方案:需要将文件从Web层次结构下载到相同层次结构下的本地驱动器.

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

示例Web层次结构:

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

下载文件1时,它应存储在路径1中-"C:\ .... \ Downloads \ Parent 1 \ Child 1 \"

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

下载文件2时,它应存储在路径2中-"C:\ .... \ Downloads \ Parent 1 \ Child 2 \"

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

问题:

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

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.

必需的解决方案:

我想使用现有的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.

即使您能够提取 ChromeDriver ChromeSession 属性,例如会话ID Cookies 和发起的浏览会话中的其他会话属性,您仍然无法更改的那些属性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() 关闭销毁 ChromeDriver Chrome浏览器实例正常运行,然后使用新的配置集覆盖一组新的 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:

  • 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

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

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