K Kreid
K Kreid

Reputation: 103

How to make headless browser visible Python

I have created a headless webdriver chrome browser by setting this argument:

chrome_options.add_argument("--headless")

and then opening the browser using:

driver = webdriver.Chrome(executable_path=os.path.abspath("chromedriver"), chrome_options=chrome_options)

Is it possible to make the browser appear once a condition is met? I have tried removing the attribute again using:

chrome_options.arguments.remove("--headless")

but that does not do anything.

Upvotes: 4

Views: 3001

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193058

As you have initiated a --headless instance by setting:

chrome_options.add_argument("--headless")

further, it won't be possible to make the Browsing Context visible again within the same session.


Reason

When you configure ChromeDriver using ChromeOptions() to initiate 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:

Upvotes: 1

Nicholas Martinez
Nicholas Martinez

Reputation: 586

You are not going to be able to remove options/arguments from your Selenium sessions after the session has been started. Each Selenium session is created using a unique session ID and runs with the parameters passed to it until asked to .quit(). You will not be able to watch your Chrome session run if you pass it the --headless option when you start it.

Upvotes: 1

Shailyn Ortiz
Shailyn Ortiz

Reputation: 766

when you pass the --headless parameter to chrome it is actually creating the instance as headless, not creating a window and hiding it, if you wanted to show the instance when a condition is meet you must consider not using --headless at the chrome params.

Upvotes: 1

Related Questions