selenium+linux+python,Linux下搭建Python3.7+Selenium+Chrome+Chromedriver

以前在Windows上进行WebUI自动化,领导要求用Docker,转战CentOS进行环境搭建,多亏了网上的各位童鞋,环境得了顺利搭建,总结一下,希望对自己和大家有所帮助!

1.进入Docker:

命令:docker exec -it XXX bash

2.由于Docker什么也没装,所以先是开始装了rz/sz,供导入文件用:

命令:yum install lrzsz

3.安装Python3.7

1) 下载python源码:https://www.python.org/

2)通过rz上传到linux上

3)先安装一些依赖软件,在此谢谢广大网友的帮助:

yum -y install zlib zlib-devel

yum -y install bzip2 bzip2-devel

yum -y install ncurses ncurses-devel

yum -y install readline readline-devel

yum -y install openssl openssl-devel

yum -y install openssl-static

yum -y install xz lzma xz-devel

yum -y install sqlite sqlite-devel

yum -y install gdbm gdbm-devel

yum -y install tk tk-devel

yum -y install libffi libffi-devel

4)解压文件:tar -xvzf Python-3.5.1.tgz

5 ) 进入目录: cd Python...

6 )配置:./configure --prefix=/usr/python --enable-shared CFLAGS=-fPIC

7)编译:make

8 ) make install

9 ) 添加动态链接库路径

cd /etc/ld.so.conf.d

vim python3.conf #添加/usr/python/lib到文件中

ldconfig #执行ldconfig命令,使修改生效

10)设置软连接

ln -s /usr/python/bin/python3.7 /usr/bin/python3

ln -s /usr/python/bin/pip3.7 /usr/bin/pip3

11)任意地方命令行输入python3

4.安装Chrome

yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

5.查看Chrome版本

google-chrome-stable -version

6.请注意接下来安装的Chromedriver一定要和Chrome版本对应上、

对应表:https://www.cnblogs.com/jinxiao-pu/p/8682126.html

Chrome下载地址:http://chromedriver.storage.googleapis.com/index.html

下载之后,rz上传到Linux

7.安装解压缩工具:yum install unzip

8.解压缩chromedriver:unzip Chromedriver

9../chromedriver

10.建立软连接ln -s /opt/google/chromedriver /usr/bin/chromedriver

11.改变权限chmod +x /usr/bin/chromedriver

12.pip3 install selenium

13.创建例子,看是否可以运行

test.py 由于Linux上是×××面运行的,所以采用下面的方法

#coding=utf8

from selenium import webdriver

from selenium.webdriver.chrome.options import Options

chrome_options = Options()

chrome_options.add_argument('--headless')

chrome_options.add_argument('--no-sandbox')

chrome_options.add_argument('--disable-gpu')

chrome_options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(chrome_options=chrome_options)

#driver = webdriver.Chrome()

driver.get("https://www.baidu.com")

print(driver.page_source)

#driver.quit()

至此环境搭建完成,下面开始写自动化代码!

你可能感兴趣的:(selenium+linux+python,Linux下搭建Python3.7+Selenium+Chrome+Chromedriver)