This article is the Amazon Linux 2 version of Building a Selenium Environment on Amazon Linux in the Shortest Time. If you want to build a Selenium environment on Amazon Linux, please refer to the above link.
With the end of security updates for Amazon Linux on June 30, 2020, You can now build a Selenium environment on Amazon Linux 2. The procedure is almost the same, but there are some changes. The following is all done as root.
・ Install ** Google Chrome ** -Installation of ** GConf2 ** * Changed -Install ** ChromeDriver ** ・ Install ** Google Noto Fonts ** -Installation of ** Selenium ** * Changed
If you do it with yum install google-chrome-stable
, you will die from the dependency.
Let's do the following:
curl https://intoli.com/install-google-chrome.sh | bash
The latest Google Chrome should work fine.
In the case of Amazon Linux, I could not install it without adding the repository, It's easy to install on Amazon Linux 2.
yum -y install GConf2
Since the currently installed Google Chrome is 79.0.3945.88
, get the corresponding 79.0.3945.36
.
You can check the version of Google Chrome with google-chrome-stable -version
.
wget https://chromedriver.storage.googleapis.com/79.0.3945.36/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
mv chromedriver /usr/local/bin/
At this rate, when I took a screenshot of the screen with Selenium Since Japanese characters are garbled, install the font. https://www.google.com/get/noto/
cd ~/Downloads #Somewhere suitable
wget https://noto-website-2.storage.googleapis.com/pkgs/Noto-hinted.zip
unzip Noto-hinted.zip
mkdir -p /usr/share/fonts/opentype/noto
cp *otf *ttf /usr/share/fonts/opentype/noto
fc-cache -f -v # optional
This is where Amazon Linux 2 became a hassle.
In Amazon Linux2, pip is not included by default, so let's install pip first.
The default python version of Amazon Linux 2 is 2.7.16
, so I really want to upgrade to 3 series before installing pip,
Since there was a concern that yum would not work, I installed it as it is.
yum install -y python-pip
After that, you can install selenium.
pip install selenium
That's all there is to install.
Create a simple test code to take a screenshot of the google top page Let's run it.
test.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--window-size=1280,1024')
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://www.google.co.jp/')
driver.save_screenshot('test.png')
Test.png is created in the execution directory.
By the way, it is an argument added to options,
・ --Headless mode
(It will not work unless it is executed with this)
・ --No-sandbox is required for google-chrome-stable to work
(You can see it by running google-chrome-stable
)
・ --Disable-gpu seems to stabilize the drawing area
(The png file created when I ran it without this was black)
---Window-size is the same as specifying the window size
It will be.
The created test.png was as follows. The characters are not garbled. I thought there would be a big change in the migration to Amazon Linux 2, but it seems that it will not affect that much.
Recommended Posts