Chrome headless immediately exits with --repl flag - google-chrome

I'm trying to use the chrome.exe headless REPL, but it seems to immediately exit.
I'm currently on Windows 7 Pro 64-bit
Chrome Version 72.0.3626.121
Command Used:
$ chrome.exe --headless --disable-gpu --enable-logging --no-sandbox --repl https://www.chromestatus.com/
Result
As you can see below it almost looks like I am able to start using the REPL, except there is no >>> .
$ [0307/131904.237:INFO:headless_shell.cc(370)] Type a Javascript expression to evaluate or "quit" to exit.
If i were to type a javascript expression:
$ [0307/132502.083:INFO:headless_shell.cc(370)] Type a Javascript expression to evaluate or "quit" to exit.
const someNumber = 1
'const' is not recognized as an internal or external command,
operable program or batch file.
$
It appears chrome has already exited. I've tried this in cmd.exe, PowerShell and ConEmu all with the same result. This is my first time with chrome headless so I apologize if the answer is obvious.

Chrome's official blog recommends two methods users can use to keep chromium from exiting after being launched from the command line. The blog explicitly and repeatedly mentions windows in their instructions so I assume they apply to the windows version of chrome as well:
Start the browser with remote debugging enabled by passing --remote-debugging-port=PORTNUM at the command line, or
Start chrome in REPL mode by passing --repl at the command line. This will cause chromium to persist as long as stdin remains open.
I've tested both approaches and can attest to both approaches working with Chromium 108.0.5359.124 on Linux as of the time of this writing. For the sake of completeness, I've included the exact commands I used to confirm this bellow:
Remote Debugging
chromium --headless --temp-profile --password-store=basic --disable-gpu --remote-debugging-port=9222 https://example.com
REPL Mode
chromium --headless --temp-profile --password-store=basic --disable-gpu --remote-debugging-port=9222 https://example.com

Related

What is the exact command for launching Chrome with remote debugging in Terminal?

I've looked up and tried a couple ways of launching Chrome with remote debugging through the terminal, and neither have worked. I get the error "no such directory" or command not found. I've tried:
chrome --remote-debugging-port=9222
and
/Applications/Google\Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
Are either of these correct? And if not, what is the right command?
One simple change is needed, add the bash shebang to the Chrome Debugger script.
#!/bin/bash
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222&

Headless Chrome in REPL mode doesn't work - spams console with an error

When I try to run Chrome in Headless, REPL mode with:
chrome --headless --disable-gpu --enable-logging --repl
I'm getting an endless spam of
{"result":{"type":"undefined"}}
It's pretty much the same problem as described here:
Headless Chrome REPL not working
But since I'm trying to run it on Windows 7, I'm not sure if that solution applies to me
Edit:
I forgot to add, I'm using a 62.0.3202.94 Chrome version

How do I use Headless Chrome in Chrome 60 on Windows 10?

I've been looking at the following article about Headless Chrome:
https://developers.google.com/web/updates/2017/04/headless-chrome
I just upgraded Chrome on Windows 10 to version 60, but when I run either of the following commands from the command line, nothing seems to happen:
chrome --headless --disable-gpu --dump-dom https://www.google.com/
chrome --headless --disable-gpu --print-to-pdf https://www.google.com/
And I'm running all of these commands from the following path (the default installation path for Chrome on Windows):
C:\Program Files (x86)\Google\Chrome\Application\
When I run the commands, something seems to process for a second, but I don't actually see anything. What am I doing wrong?
Thanks.
Edit:
As noted by Mark Rajcok, if you add --enable-logging to the --dump-dom command, it works. Also, the --print-to-pdf command works as well in Chrome 61.0.3163.79, but you'll probably have to specify a different path for the output file in order to have the necessary permissions to save it.
As such, the following two commands worked for me:
"C:\Program Files (x86)\Google\Chrome\Application\chrome" --headless --disable-gpu --enable-logging --dump-dom https://www.google.com/
"C:\Program Files (x86)\Google\Chrome\Application\chrome" --headless --disable-gpu --print-to-pdf=D:\output.pdf https://www.google.com/
I guess the next step is being able to step through the dumped DOM like PhantomJS with DOM selectors and whatnot, but I suppose that's a separate question.
Edit #2:
For what it's worth, I recently came across a Node API for Headless Chrome called Puppeteer (https://github.com/GoogleChrome/puppeteer), which is really easy to use and delivers all the power of Headless Chrome. If you're looking for an easy way to use Headless Chrome, I highly recommend it.
This works for me:
start chrome --enable-logging --headless --disable-gpu --print-to-pdf=c:\misc\output.pdf https://www.google.com/
... but only with "start chrome" and "--enable-logging" and with a path (for the pdf) specified - and if the folder "misc" exists on the c-directory.
Addition: ... the path for the pdf - "c:\misc" above - can of course be replaced with any other folder/dir.
With Chrome 61.0.3163.79, if I add --enable-logging then --dump-dom produces output:
> "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --enable-logging --headless --disable-gpu --dump-dom https://www.chromestatus.com
<body class="loading" data-path="/features">
<app-drawer-layout fullbleed="">
...
</script>
</body>
If you want to programatically control headless Chrome, here's one way to do it with Python3 and Selenium:
In an Admin cmd window, install Selenium for Python:
C:\Users\Mark> pip install -U selenium
Download ChromeDriver v2.32 and extract it. I put the chromedriver.exe in C:\Users\Mark, which is where I put this headless.py Python script:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("headless") # remove this line if you want to see the browser popup
driver = webdriver.Chrome(chrome_options = options)
driver.get('https://www.google.com/')
print(driver.page_source)
driver.quit() # don't miss this, or chromedriver.exe will keep running!
Run it in a normal cmd window:
C:\Users\Mark> python headless.py
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" ...
... lots and lots of stuff here ...
...</body></html>
Current versions (68-70) seem to require --no-sandbox in order to run, without it they do absolutely nothing and hang in the background.
The full commands I use are:
chrome --headless --user-data-dir=tmp --no-sandbox --enable-logging --dump-dom https://www.google.com/ > file.html
chrome --headless --user-data-dir=tmp --no-sandbox --print-to-pdf=whatever.pdf https://www.google.com/
Using --no-sandbox is a pretty bad idea and you should use this only for websites you trust, but sadly it's the only way of making it work at all.
--user-data-dir=... uses the specified directory instead of the default one, which is likely already in use by your regular browser.
However, if you're trying to make a PDF from HTML, then this is fairly useless, since you can't remove header and footer (containing text like file:///...) and the only viable solution is to use Puppeteer.
You should be good. Check under the Chrome Version directory
C:\Program Files (x86)\Google\Chrome\Application\60.0.3112.78
For the command
chrome --headless --disable-gpu --print-to-pdf https://www.google.com/
C:\Program Files (x86)\Google\Chrome\Application\60.0.3112.78\output.pdf
Edit:
Still execute commands where the chrome executable is, in this instance
C:\Program Files (x86)\Google\Chrome\Application\
I know this question is for Windows, but since Google gives this post as the first search result, here's what works on Mac:
Mac OS X
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --headless --dump-dom 'http://www.google.com'
Note you MUST put the http or it won't work.
Further tips
To indent the html (which is highly desirable in real pages that are bloated), use tidy:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --headless --dump-dom 'http://www.google.com' | tidy
You can get tidy with:
brew install tidy
If you want to dodge on the problem in general, and just use a service of some kind to do the work for you, I'm the author/founder of browserless which attempts to tackle running headless Chrome in a service-like fashion. Other than that it's pretty tough to keep up with the changes and making sure all the appropriate packages and resources are installed to get Chrome running, but definitely doable.
I solved it by running this (inside chrome.exe directory),
start-process chrome -ArgumentList "--enable-logging --headless --disable-gpu --print-to-pdf=c:\users\output.pdf https://www.google.com/"
you can choose your own path.print-to-pdf=<<custom path>>

run chrome 30 without sandbox

I'm trying to run chrome 30, in Linux, from the command line (executing google-chrome file of the version downloaded and unpacked in a folder).
If i run it without the option --no-sandbox the result is the following
[24419:24419:1016/012228:FATAL:zygote_host_impl_linux.cc(142)] The SUID sandbox helper binary was found, but is not configured correctly. Rather than run without sandboxing I'm aborting now. You need to make sure that /home/user/user.browsers/chrome-30/opt/google/chrome/chrome-sandbox is owned by root and has mode 4755.
./chrome-30.sh: line 5: 24419 Aborted (core dumped) $CHROME_FOLDER/google-chrome
If I run it with the option --no-sandbox, following also this procedure to configure it, the result is the error:
È stata creata una nuova finestra nella sessione corrente del browser.
[1016/012454:ERROR:nacl_helper_linux.cc(269)] NaCl helper process running without a sandbox!
Most likely you need to configure your SUID sandbox correctly
This last procedure works fine with chrome v31.
Any help?
If you have root access, you should set up the SUID sandbox as per these instructions, and run without the --no-sandbox option.
If you don't have root access, you cannot set up a SUID sandbox and you have to use the --no-sandbox option, but only use it for testing because it is a security risk.
In your case, it looks like you tried running a new Chrome window with --no-sandbox while another session was already running (that's what the message in Italian says). Try closing the existing session before starting a new one. To close the existing session, find the Chrome icon in the system tray, right click it and choose Exit. If you don't see the system tray icon, you can also try killall google-chrome or killall chromium, depending on which version you're using.
This error message...
[24419:24419:1016/012228:FATAL:zygote_host_impl_linux.cc(142)] The SUID sandbox helper binary was found, but is not configured correctly.
Rather than run without sandboxing I'm aborting now.
You need to make sure that /home/user/user.browsers/chrome-30/opt/google/chrome/chrome-sandbox is owned by root and has mode 4755.
./chrome-30.sh: line 5: 24419 Aborted (core dumped) $CHROME_FOLDER/google-chrome
...implies that the program was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session.
As per the discussion in Issue 400842: Chromium, if you have installed Chromium v35 (or later) and your cpu doesn't have the SSE2 instruction set, e.g. AthlonXP, Pentium III, etc, then Chromium can't be initiated successfully. All the new Chromium browsers from version 35 onwards need SSE2. Any Chromium version earlier than 35 should run on such a system without any issue.
To enforce these measures, Chromium team have closed the issue as Status: WontFix and mentioned:
If you really want to build Chromium without SSE2, you are welcome to apply your own patches and build your own browser however you like, but the Chromium project is not accepting patches to build without SSE2.
Further, this feature was successfully tested in AMD Athlon 3400+ desktop with Lubuntu 14.04.2 and Chrome seems to work fine. But on an older AMD Athlon 1300 without SSE, Chrome now courteously displays a message that "my hardware does not support it".
In mac you can run the below command to run the chrome in sandbox mode. This will open the chrome and supress the security settings. This can be used for testing API's etc from angular or ionic
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --disable-web-security --user-data-dir=$HOME/chrome-in-sandbox-mode

chrome.exe command line parameters for kiosk mode

I'm using chrome.exe version: 29.0.1547.66 m on a w7 pc.
I'd like to use command line parameters like --kiosk from the command prompt when starting chrome.
I found a list of switches here: http://peter.sh/experiments/chromium-command-line-switches/
When I type in:
chrome.exe --kiosk http://somesite.com
Chrome starts, it opens up the specified url, but not in kiosk mode.
It seems to ignore the --kiosk command. If I try --help as a flag, it gets ignored as well.
Is this supported on chrome.exe on windows? Should the commandline interface be enabled first somehow? Any thoughts?
Thanx in advance,
Paul
Problem solved ... also thanks to the post of ComFreek .... problem was about running instances of chrome.
As soon as I closed all running chrome instances before invoking chrome, the commandline options started to work.
--chrome-frame worked for me:
"c:\progra~2\Google\Chrome\Application\chrome.exe" --chrome-frame --kiosk http://foldoc.org/pub/misc/automata.html