before I made a periodical execution process with Selenium and Python I did, but it stopped working before I knew it ... So, even if I build the environment again with the latest firefox and selenium, it doesn't work. (Need geckodriver? Implicitly_wait doesn't work? Centos6 doesn't work? Can't run in parallel? I've followed various steps, but I forgot, so I'll omit the details.)
So, I switched to CentOS 7 and Chrome and tried to build the environment.
Selenium
selenium installation
yum install python-pip
pip install selenium
Xvfb
Xvfb installation
yum install xorg-x11-server-Xvfb
Xvfb service creation
vim /usr/lib/systemd/system/Xvfb.service
/usr/lib/systemd/system/Xvfb.service
[Unit]
Description=Virtual Framebuffer X server for X Version 11
[Service]
Type=simple
EnvironmentFile=-/etc/sysconfig/Xvfb
ExecStart=/usr/bin/Xvfb $OPTION
ExecReload=/bin/kill -HUP ${MAINPID}
[Install]
WantedBy=multi-user.target
Create Xvfb environment variable configuration file
vim /etc/sysconfig/Xvfb
/etc/sysconfig/Xvfb
# Xvfb Enviroment File
OPTION=":1 -screen 0 1366x768x24"
Start Xvfb service
systemctl enable Xvfb
systemctl start Xvfb
Chrome
Create Chrome repository file
vim /etc/yum.repos.d/google-chrome.repo
bash:/etc/yum.repos.d/google-chrome.repo
[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
google-chrome-stable installation
yum install google-chrome-stable
For Japanese display
Japanese font installation
yum install ipa-pgothic-fonts.noarch
ChromeDriver - WebDriver for Chrome : Downloads Download the appropriate driver from. This time version 2.29.
Chrome Driver installation
wget http://chromedriver.storage.googleapis.com/2.29/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
mv chromedriver /usr/local/bin
rm chromedriver_linux64.zip
I tried to incorporate the above construction contents into the configuration management tool "itamae". If you like, please. => https://github.com/kotanbo/itamae-selenium-centos7 (The pattern of CentOS6 is described in some (Xvfb), but I gave up on building with CentOS6 ...)
ChromeDriver - WebDriver for Chrome : Getting started The following is a sample with reference to.
python sample
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import os, time, datetime, re, util, sys
import selenium.webdriver.chrome.service as service
class Sample():
def __init__(self):
self.aservice = service.Service('/usr/local/bin/chromedriver')
self.aservice.start()
capabilities = {'chrome.binary': '/usr/bin/google-chrome-stable', "chromeOptions": {"args": ['--no-sandbox']}}
self.driver = webdriver.Remote(self.aservice.service_url, capabilities)
self.driver.implicitly_wait(3)
self.base_url = "http://example.jp/"
def __del__(self):
self.driver.close()
def sample(self):
driver = self.driver
try:
driver.get(self.base_url + "/home")
driver.find_element_by_xpath("//a[@id='LoginButton']/img").click()
except:
finally:
sample = Sample()
sample.sample()
cron sample
0 9 * * * export DISPLAY=localhost:1.0; python /root/selenium/sample.py
0 9 * * * export DISPLAY=localhost:1.0; python /root/selenium/sample.py
0 4 * * * pkill -KILL -f chromedriver
Start Xvfb as a daemon with systemd Install Chrome on CentOS 7
Recommended Posts