一、selenium 环境配置
1、下载浏览器
目前比较常用的浏览器是 Google Chrome 浏览器,所以本教程以 chrome 为主,后面简介一下其他浏览器的环境配置。
chrome 下载: www.google.cn/chrome/
2、chromedriver 环境配置
chromedriver 是chromedriver提供的浏览器驱动程序,他的作用是将代码的请求转发给浏览器。
(1)chromedriver 下载
chromedriver 国内下载地址:chromedriver.storage.googleapis.com/index.html
在下载 chromedriver 的时候需要注意的是chromedriver 和 chrome 浏览器版本的对应关系
(2)chrome 浏览器版本查看:

这里的chrome 的版本是90.0.4430.212。在下载chromedriver 的时候就需要下载对应的版本。

这里使用的是90.0.4430.24(大版本对应也行即版本为90版本)

根据自己的系统选择chromedriver,windows 只有32位的,因为Windows 64为也支持运行32为的。点击下载保存并进行解压,记下chromedriver 的路径

(3)配置环境变量
在搜索界面打开编辑系统环境变量。


然后点击确定,保存即可。
3、验证
win + r 输入 cmd

在cmd里面输入 chromedriver --version

python
C:\Users\16478>chromedriver --version
ChromeDriver 90.0.4430.24 (4c6d850f087da467d926e8eddb76550aed655991-refs/branch-heads/4430@{#429})
二、安装 selenium 工具包
这里使用的是 python 的包管理工具 pip
python
pip install selenium
其他浏览器环境配置
其他环境与chrome浏览器的区别就在于 driver 的配置, 所以下载对应浏览器的driver即可
各浏览器的下载地址和对照表:
www.selenium.dev/documentati...

配置 Selenium
下载浏览器
下载浏览器驱动
Chrome
根据浏览器的版本号去下载对应的 chromedriver。
chromedriver与chrome的对应关系表:huilansame.github.io/huilansame....
chrome版本与对应的谷歌驱动(chromedriver):www.cnblogs.com/yfacesclub/...
chromedriver:(chromedriver.storage.googleapis.com/index.html)
淘宝 chromedriver 镜像:(npm.taobao.org/mirrors/chr...)
Firefox
geckodriver:(github.com/mozilla/gec...)

根据使用的操作系统下载相应的 geckodriver。
安装 Selenium
安装到新建的 selenium 测试项目
python
pip install selenium
配置Chromedriver的环境变量
Mac
python
vim ~/.bash_profile
export PATH=$PATH:[chromedriver所在路径]
Windows
- 在环境变量中配置

-
重启命令行以及pycharm
-
验证是否配置成功

Selenium Grid2
可以部署到有 docker 环境的电脑。
Selenium Standalone Server:(www.seleniumhq.org/download/)
Selenium Docker:github.com/SeleniumHQ/...)
此处链接只是参考,按照下面步骤操作。
安装 docker-compose
python
pip install docker-compose
新建 docker-compose.yml 文件
python
vim docker-compose.yml
python
# To execute this docker-compose yml file use `docker-compose -f <file_name> up`# Add the `-d` flag at the end for detached executionversion: "3"services: selenium-hub: image: selenium/hub:3.141.59-lithium container_name: selenium-hub environment: - GRID_MAX_SESSION=10 # - newSessionWaitTimeout=25000 - JAVA_OPTS=-Xmx512m # - SE_OPTS="-debug" ports: - "4444:4444" chrome: image: selenium/node-chrome-debug:3.141.59-lithium volumes: - /dev/shm:/dev/shm depends_on: - selenium-hub environment: - HUB_HOST=selenium-hub - HUB_PORT=4444 - NODE_MAX_INSTANCES=10 - NODE_MAX_SESSION=10 - SCREEN_WIDTH=1366 - SCREEN_HEIGHT=768 - SCREEN_DEPTH=24 ports: - "5900:5900" firefox: image: selenium/node-firefox-debug:3.141.59-lithium volumes: - /dev/shm:/dev/shm depends_on: - selenium-hub environment: - HUB_HOST=selenium-hub - HUB_PORT=4444 - NODE_MAX_INSTANCES=10 - NODE_MAX_SESSION=10 - SCREEN_WIDTH=1366 - SCREEN_HEIGHT=768 - SCREEN_DEPTH=24 ports: - "5901:5900"
启动
python
docker-compose up -d
访问
http://localhost:4444/grid/console
最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:
这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!
