Notes on Installing chromedriver

2018-08-16 15:07 (6 years ago) ytyng

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

Chrome

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

chromedriver

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/

Python Library

pyvirtualdisplay is required

sudo apt install xvfb
pip install selenium chromedriver pyvirtualdisplay


Test Code

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')

Extra: Viewing Images on the Server

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.

Note for Mac: Use binaries downloaded from the site

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.

Troubleshooting

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.

Current rating: 2

Comments

Archive

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011