How to Set Selenium Webdriver from Headless Mode to Normal Mode Within the Same Session

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

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

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

How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?

So after correcting my code to:

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"])
options.add_argument('--disable-gpu')
options.add_argument('--headless')
chrome_driver_path = "C:\Python27\Scripts\chromedriver.exe"

The .exe file still came up when running the script. Although this did get rid of some extra output telling me "Failed to launch GPU process".

What ended up working is running my Python script using a .bat file

So basically,

  1. Save python script if a folder
  2. Open text editor, and dump the following code (edit to your script of course)

    c:\python27\python.exe c:\SampleFolder\ThisIsMyScript.py %*

  3. Save the .txt file and change the extension to .bat

  4. Double click this to run the file

So this just opened the script in Command Prompt and ChromeDriver seems to be operating within this window without popping out to the front of my screen and thus solving the problem.

Selenium: Disable headless when code is running? (Python)

No, it won't be possible to initialize google-chrome headlessly and then make it visible to solve the captcha.

When you configure ChromeDriver using ChromeOptions() to initiate headless 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 remains uneditable. So you modify the ChromeOptions of 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 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.



References

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
  • How do I make Chrome Headless after I login manually

Is this possible to headless mode after opening chrome normal mode?

Once the session is created you can't

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 is 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.

You can find a couple of relevant discussions in:
How can I switch from headless mode to normal mode using Google Chrome and Selenium??

How do I enter a type command onto my selenium script and also turn it into headless mode?

You can send the character sequence to the Google Search area using the following locator strategy:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("ONS data")

Note: You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

But once you have initiated google-chrome in headed mode you won't be able to shift to google-chrome-headless mode within the same session.

You can find a relevant detailed discussion in How can I switch from headless mode to normal mode using Google Chrome and Selenium?



Related Topics



Leave a reply



Submit