写在前面: chrome不能再linux下以root的权限运行
1、修改yum源
在/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
2、 chrome安装和chromedriver下载
chrome下载安装
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
chromedriver下载,我下载的是最新版本。chrome也是最新版本, chromedriver的驱动必须与google的版本对应
https://npm.taobao.org/mirrors/chromedriver/2.40/chromedriver_linux64.zip
运行chrome
找到chrome路径,并做个软连接,方便使用:
which google-chrome-stable
ln -s xxx /bin/chrome
修改chroemdriver的文件权限
chmod 777 chromedriver_path
3、安装Selenium
pip3 install selenium
4 、Chrome不能以Root用户运行,需要添加将启动方式修改为沙盒.
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
driver = webdriver.Chrome(executable_path="/root/test/chromedriver", chrome_options=options)
此时运行程序会报错:
# vi /usr/bin/google-chrome
在exec -a "$0" "$HERE/chrome" "[email protected]"后他添加
--no-sandbox --user-data-dir
结果为:exec -a "$0" "$HERE/chrome" "[email protected]" --no-sandbox