8

I know this is sort of counter to the purpose of headless automation, but...

I've got an automation test running using Selenium and Chromedriver in headless mode. I'd prefer to keep it running headless, but occasionally, it runs into an error that really needs to be looked at and interacted with. Is it possible to render and interact with a headless session? Maybe by duplicating the headless browser in a non-headless one? I can connect through remote-debugging, but the Dev Tools doesn't seem to do allow me to view the rendered page or interact with anything.

I am able to take screenshots, which sort of helps. But I'm really looking for the ability to interact--there's some drag-and-drop elements that aren't working well with Selenium that are causing issues occasionally.

  • can't you commenting out the `options.add_argument('headless')` whenever you don't want it to be headless? – Omar Einea Jan 26 '18 at 19:12
  • @OmarEinea I need to deal with the error mid-session. So, I'm hoping there's a way to start headless, then when needed, open/render the same session at the same point, interact as needed, then go headless again. I know you can attach to an existing session, but I can't get it to render/duplicate it. –  Jan 26 '18 at 19:29
  • So even screenshots won't work, right? – Omar Einea Jan 26 '18 at 19:33
  • can you do "browser.save_screenshot"? Also, maybe try logging your browser.info and look at the html? – SteveJ Jan 26 '18 at 19:33
  • @SteveJ: I am able to take screenshots, which sort of helps. But I'm really looking for the ability to interact--there's some drag-and-drop elements that aren't working well with Selenium that are causing issues occasionally. –  Jan 26 '18 at 19:38
  • @needalittlehelp Update your last comment details within the question. These bytes may be helpful in constructing an answer. – undetected Selenium Jan 26 '18 at 19:46
  • @needalittlehelp I searched through source code, the only place I can find to pass the headless option is in the constructor - which won't do you much good I'm afraid. – SteveJ Jan 26 '18 at 19:57

3 Answers3

12

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
Kyle
  • 685
  • 7
  • 14
  • 3
    Thanks! if you want to connect to the debugged browser from external machine you should add one more argument chrome_options.add_argument('--remote-debugging-address=0.0.0.0') or another external IP address. and then you can go to http://extenral_server_ip:9222 Another important thing to mention is that if you get blank console page, you probably should allow insecure content on site settings of chrome (click on the small lock icon on the address bar). – Avishay116 Jun 07 '20 at 12:31
  • 1
    Oh wow, I never thought to use 0.0.0.0 to debug on an external machine. I've been using SSH and connecting to the port by binding to it through ssh -L. That's a great tip, thank you as well! – Kyle Jun 13 '20 at 00:20
1

What you are asking for is currently not possible. Further, such a "feature" would have nothing to do with Selenium, but the vendor of the browser. You can search their bug tracker to see if such a feature has already been requested.

The only currently available option is to run full GUI browser during debug / development of your tests.

SiKing
  • 10,003
  • 10
  • 39
  • 90
0

No, it is not possible to open/display/render a headless Selenium session.

Following are the steps you can take as per your situation/requirement :

  • Chromedriver in headless mode occasionally it runs into an error : Put the error prone code block in a try-except block and debug the root cause. You can take a screenshot as well.

  • Can I connect through remote-debugging : No you won't be able to connect to any existing session. A detailed discussion here.

  • Drag-and-Drop elements that aren't working well : Get the page source and examine the elements and decide a proper Locator Strategy

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352