'Possible to open/display/render a headless Selenium session?
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.
Solution 1:[1]
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:
- Check the box for "Discover network Targets"
- Hit "Configure..."
- Add the link "localhost:9222" to the list of servers
Solution 2:[2]
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.
Solution 3:[3]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | SiKing |
Solution 3 | undetected Selenium |