Ubuntu 16.04
Reference Running Google Chrome in headless mode on EC2 Ubuntu and taking screenshots - Qiita
python 2.7 - Unknown error: Chrome failed to start: exited abnormally - Stack Overflow
curl -O https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb
sudo apt update
sudo apt -f install -y
Test
cd /tmp
google-chrome --headless --disable-gpu --screenshot https://www.example.com/
imgcat screenshot.png # imgcat will be explained later
curl https://chromedriver.storage.googleapis.com/LATEST_RELEASE
curl -O https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
sudo mv chromedriver /usr/local/bin/
Test
chromedriver --verbose &
curl -d '{ "desiredCapabilities": {"browserName": "chrome", "chromeOptions": {"args": [ "--headless" ]} }}' http://127.0.0.1:9515/session
A sessionId will be returned
By the way, if you don't add --headless
xvfb-run chromedriver --verbose &
you will need to do this
Open a page
curl -d '{"url":"https://www.example.com"}' http://127.0.0.1:9515/session/<session-id>/url
Screenshot
curl http://127.0.0.1:9515/session/<session-id>/screenshot | python3 -c "import sys, json; print(json.load(sys.stdin)['value'])"|base64 -d |imgcat
※ Execute the screenshot command ➝ The result will be returned as JSON containing Base64 ➝ Parse with Python ➝ Decode to binary ➝ Display on iterm2 with imgcat
If it doesn't work, try --no-sandbox and --disable-dev-shm-usage
Note: Disable web security with --ignore-certificate-errors and --disable-web-security
Reference: https://www.pawangaria.com/post/automation/browser-automation-from-command-line/
pyvirtualdisplay is required
sudo apt install xvfb
pip install selenium chromedriver pyvirtualdisplay
from pyvirtualdisplay import Display
display = Display(visible=0, size=(1280, 800))
display.start()
from selenium.webdriver import Chrome, ChromeOptions
options = ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = Chrome(options=options)
driver.get('https://example.com')
driver.get_screenshot_as_file('/tmp/screenshot.png')
If you are using iTerm
https://gist.github.com/wesbos/eac5f93478002312db1f This script
sudo curl -o /usr/local/bin/imgcat -O https://raw.githubusercontent.com/gnachman/iTerm2/master/tests/imgcat && sudo chmod +x /usr/local/bin/imgcat
Install imgcat on the server,
imgcat /tmp/screenshot.png
and the image will be displayed in the terminal.
If you are using chromedriver installed via brew on a Mac, when you use the get method
data:;
the URL opens and
raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response
it may stop with this error.
brew uninstall --force chromedriver
Uninstall chromedriver with the above command,
http://chromedriver.chromium.org/downloads download the latest Mac binary from here, scan it for viruses, and then place it in /usr/local/bin/
for use.
On Ubuntu
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 4.15.0-1023-aws x86_64)
appears
https://stackoverflow.com/questions/50790733/unknown-error-devtoolsactiveport-file-doesnt-exist-error-while-executing-selen Refer to this
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
options.addArguments("start-maximized"); // open Browser in maximized mode
options.addArguments("disable-infobars"); // disabling infobars
options.addArguments("--disable-extensions"); // disabling extensions
options.addArguments("--disable-gpu"); // applicable to windows os only
options.addArguments("--no-sandbox"); // Bypass OS security model
WebDriver driver = new ChromeDriver(options);
driver.get("https://google.com");
Try adding these options. Note that this code is not in Python.
For me, adding disable-infobars
stopped the error.
Comments