简体   繁体   中英

Let selenium display browser while in --headless

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM