Change Chromeoptions in an Existing Webdriver

passing ChromeOptions without instantiating new WebDriver

You cannot do this without altering your TestExtension class. The reason being whatever arguments you are passing gets passed to the browser being spawned at the time of instantiation. After that there is no way of altering anything to change the behavior of the spawned browser. You would need to alter your TestExtension class and then provide in a mechanism wherein a user can basically inject their own capabilities as well, which would be considered by TestExtension prior to spawning the browser. It could be as trivial as passing in the fully qualified package name of the class which when invoked can instantiate the capability object that you pass via a JVM argument.

Your TestExtension class would basically inspect the JVM argument for any custom capabilities being passed and if found, it would merge those capabilities as well into its capabilities and then spawn the browser. That is the only way of doing this.

Python, Selenium: How to specify which sites can load images?

No, you won't be able to configure ChromeOptions differently per site unless you reinitialize ChromeDriver and google-chrome

When you configure an instance of a ChromeDriver using ChromeOptions() in the process of initiating a new Chrome Browsing Session the configuration gets baked into the ChromeDriver service instance and will persist till the lifetime of the WebDriver being uneditable. So you can't add any further ChromeOptions to the WebDriver instance which is currently in execution.

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

A cleaner way would be to call driver.quit() within tearDown(){} method to close and destroy the current 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:

  • Change ChromeOptions in an existing webdriver

How can I switch from headless mode to normal mode using Google Chrome and Selenium?

No, it won't be possible to open google-chrome in headless mode and moving forward shift to the headed mode.



Deep Dive

When you configure an instance of a ChromeDriver using ChromeOptions(), in the process of initiating a new Chrome Browsing Session the configuration gets baked into the chromedriver executable and will persist till the lifetime of the WebDriver and being uneditable. So you can't add any further ChromeOptions to the WebDriver instance which is currently in execution.

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

A cleaner way would be to call driver.quit() within tearDown(){} method to close and destroy the current 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?
  • Change ChromeOptions in an existing webdriver


Related Topics



Leave a reply



Submit