1

Is there a way to run selenium headless but have it display the window for example at the start of the application ? like:

show browser login do captcha go --headless execute tasks

  • To my limited knowledge, this could not be done, for you provide the option arguments while instantiating the driver, which implies that you first set the `--headless` argument, and then instantiate the browser. You may not change the browser mode later for that instance. – Anand Gautam Feb 05 '22 at 13:06
  • 1
    hey, just found this and thought may help you. Please check: [SO Archive Link](https://stackoverflow.com/questions/48467961/possible-to-open-display-render-a-headless-selenium-session) – Anand Gautam Feb 05 '22 at 13:17

1 Answers1

0

Actually, this is possible.

You can peek into a headless browser by using the "--remote-debugging-port" argument in ChromeOptions. For example,

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--remote-debugging-port=9222') # Recommended is 9222
driver = webdriver.Chrome(options=chrome_options)

Then while Selenium is running, you can go to http://localhost:9222 in your regular browser to view the headless session. Your browser will display links to each tab Chrome has open, and you can view/interact with each page by clicking on the links. You'll also be able to monitor Selenium/Chrome as Selenium runs its test.

Edit: As of Chrome 100 or so, you now need to go to the link chrome://inspect to view the headless session in your browser while Selenium is running.

You'll need to do some extra configuration as well. Basically:

  1. Check the box for "Discover network Targets"
  2. Hit "Configure..."
  3. Add the link "localhost:9222" to the list of servers

CREDITS: https://stackoverflow.com/a/58045991/1136132

joseantgv
  • 1,943
  • 1
  • 26
  • 34