Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set selenium webdriver from headless mode to normal mode within the same session?

Is it possible after setting selenium webdriver to a headless mode set it back to a normal mode?

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver.get(http://stackoverflow.com)

# set driver back to normal mode
like image 925
Mykola Zotko Avatar asked Feb 16 '19 09:02

Mykola Zotko


People also ask

What is happening when the browser is running in headless mode?

You heard it right, it is called “headless”), it means a browser without a user interface or “head.” So, when the browser is headless, the GUI is hidden. Therefore, when you use a headless browser to access any website, you can't see anything. However, the program runs in the background.

How do I use Selenium in headless mode?

You can run Google Chrome in headless mode simply by setting the headless property of the chromeOptions object to True. Or, you can use the add_argument() method of the chromeOptions object to add the –headless command-line argument to run Google Chrome in headless mode using the Selenium Chrome web driver.

Can we use Selenium to work with an already open browser session?

We can connect to an already open browser using Selenium webdriver. This can be done using the Capabilities and ChromeOptions classes. The Capabilities class obtains the browser capabilities with the help of the getCapabilities method.


1 Answers

No, it won't be possible to make Chrome operate initially in headless mode and then switch back to normal mode within the same session.

When you configure an instance of a ChromeDriver with ChromeOptions() to span a new Chrome Browsing Context the configuration gets baked within the chromedriver executable which will persist for the lifetime of the WebDriver and being uneditable. So you can't modify/add any existing/new configuration through ChromeOptions() class 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 do I make Chrome Headless after I login manually
like image 88
undetected Selenium Avatar answered Oct 23 '22 11:10

undetected Selenium