Skip to content

Instantly share code, notes, and snippets.

@takaya1992
Last active July 14, 2023 16:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takaya1992/6fc6878fb936559344fac068ab6e90f2 to your computer and use it in GitHub Desktop.
Save takaya1992/6fc6878fb936559344fac068ab6e90f2 to your computer and use it in GitHub Desktop.
Docker ( AmazonLinux2 ) 上で Perl から Headless Chrome を操作するサンプル

Docker ( AmazonLinux2 ) 上で Perl から Headless Chrome を操作するサンプル

HOW TO USE

ローカル

$ docker-compose build
$ docker-compose run --rm app /bin/bash

Docker コンテナの bash

bash $ carton install
bash $ carton exec -- perl selenium_chrome_test.pl
requires 'Selenium::Remote::Driver';
version: '3.7'
volumes:
app_perl_local:
driver: local
services:
app:
image: app
build:
context: ./
dockerfile: Dockerfile
command: perl -v
ports:
- '3000:3000'
volumes:
- ./:/app
- app_perl_local:/app/local
FROM amazonlinux:2
WORKDIR /app
RUN yum update -y \
&& yum install -y perl perl-core perl-App-cpanminus gcc expat-devel \
&& rm -rf /var/cache/yum/* \
&& yum clean all \
&& cpanm Carton
COPY google-chrome.repo /etc/yum.repos.d/google-chrome.repo
RUN yum install -y google-chrome-stable unzip wget lsof ipa-gothic-fonts ipa-mincho-fonts
RUN CHROME_MAJOR_VERSION=$(google-chrome --version | sed -E "s/.* ([0-9]+)(\.[0-9]+){3}.*/\1/") \
&& CHROME_DRIVER_VERSION=$(wget --no-verbose -O - "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_${CHROME_MAJOR_VERSION}") \
&& echo "Using chromedriver version: "$CHROME_DRIVER_VERSION \
&& wget --no-verbose -O /tmp/chromedriver_linux64.zip https://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip \
&& unzip /tmp/chromedriver_linux64.zip chromedriver -d /usr/local/bin/ \
&& rm /tmp/chromedriver_linux64.zip
[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
use strict;
use warnings;
use utf8;
use feature qw/say/;
use Selenium::Chrome;
# Selenium を介さず直接 chromedriver 経由で Chrome を操作する
my $driver = Selenium::Chrome->new(
extra_capabilities => {
'goog:chromeOptions' => {
args => [ 'headless', 'disable-gpu', 'window-size=1920,1080', 'no-sandbox' ],
}
}
);
$driver->get('https://www.google.com');
say $driver->get_title(); # => Google
$driver->shutdown_binary();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment