问题
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.
回答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.
回答2:
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
回答3:
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. It will display links to each tab Chrome has open, and you can view/interact with each page by clicking on the links. From there, you'll be able to monitor Selenium/Chrome as Selenium runs its automation test.
来源:https://stackoverflow.com/questions/48467961/possible-to-open-display-render-a-headless-selenium-session