Linux-Centos下selenium部署
1. 安装chrome
1.1 添加repo源
sudo vi /etc/yum.repos.d/google.repo
在打开的空文件中填入以下内容
[google] name=Google-x86_64 baseurl=http://dl.google.com/linux/rpm/stable/x86_64 enabled=1 gpgcheck=0 gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
1.2 yum安装
sudo yum update sudo yum install google-chrome-stable
1.3安装必要的库
yum install mesa-libOSMesa-devel gnu-free-sans-fonts wqy-zenhei-fonts
2. 安装chromedriver
2.1查看chrome版本号
google-chrome --version
2.2 下载chromedriver
请注意chrome和chromedriver的区别,前者是浏览器,后者是其驱动,而二者缺一不可。https://www.cnblogs.com/zwnsyw/p/13387104.html有各个版本的chromedriver,而你要选择的版本要与你的chrome版本对应
可先下载在windows在上传linux
2.3 添加权限(一定要添加,不然使用的时候会报错)
chmod +x /usr/bin/chromedriver
3. 安装selenium
pip 安装selenium
pip install selenium
通过清华镜像快速安装 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ selenium pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ selenium
4.脚本测试
将chromedriver放在Python脚本同一目录(记得添加权限)
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
DRIVER_PATH = ‘./chromedriver‘
if __name__ == "__main__":
# 设置浏览器
options = Options()
options.add_argument(‘--no-sandbox‘)
options.add_argument(‘--headless‘) # 无头参数
options.add_argument(‘--disable-gpu‘)
# 启动浏览器
driver = Chrome(executable_path=DRIVER_PATH, options=options)
# 访问目标URL
driver.get(‘https://www.baidu.com/‘)
print(driver.page_source)
driver.save_screenshot("000.png") # 截图
driver.close()
driver.quit()至此,恭喜你已经部署成功!
相关推荐
curiousL 2020-07-18
王练 2020-07-18
Reiki 2020-06-12
xiangxiaojun 2020-09-23
Reiki 2020-08-16
letheashura 2020-08-14
tiankele0 2020-07-18
amei0 2020-07-08
Reiki 2020-07-06
Ronnyxie 2020-07-06
xiangxiaojun 2020-07-05
zhanghaibing00 2020-06-28
xiongyouqiang 2020-06-28
Ronnyxie 2020-06-27
amei0 2020-06-26
letheashura 2020-06-26