1.安装selenium,在终端执行如下命令,密码是电脑开机登陆密码:
localhost:~ wangyadong$ sudo pip install -U selenium
Password:
WARNING: The directory '/Users/wangyadong/Library/Caches/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting selenium
Downloading selenium-3.141.0-py2.py3-none-any.whl (904 kB)
|████████████████████████████████| 904 kB 23 kB/s
Requirement already satisfied, skipping upgrade: urllib3 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from selenium) (1.25.6)
Installing collected packages: selenium
Successfully installed selenium-3.141.0
2.安装chrome浏览器和浏览器驱动chromedriver
2.1查看Chrome版本:右上角->帮助->关于Google Chrome
2.2 查找Chrome和ChromeDriver的对应关系参考:Chrome73以上版本,ChromeDriver版本与Chrome版本均保持一致;Chrome73以下版本,可参考以下关系表
Chrome版本 | ChromeDriver版本 |
v73、74... | v73、V74... |
v71-73 | v2.46 |
v70-72 | v2.45 |
v69-71 | v2.44 |
v69-71 | v2.43 |
v68-70 | v2.42 |
v67-69 | v2.41 |
v66-68 | v2.40 |
v66-68 | v2.39 |
v65-67 | v2.38 |
v64-66 | v2.37 |
v64-66 | v2.36 |
v62-64 | v2.35 |
v61-63 | v2.34 |
v60-62 | v2.33 |
v59-61 | v2.32 |
v58-60 | v2.31 |
v58-60 | v2.30 |
v56-58 | v2.29 |
v55-57 | v2.28 |
v54-56 | v2.27 |
v53-55 | v2.26 |
v53-55 | v2.25 |
v52-54 | v2.24 |
v51-53 | v2.23 |
v49-52 | v2.22 |
v46-50 | v2.21 |
v43-48 | v2.20 |
v43-47 | v2.19 |
v43-46 | v2.18 |
v42-43 | v2.17 |
v42-45 | v2.16 |
v40-43 | v2.15 |
v39-42 | v2.14 |
v38-41 | v2.13 |
v36-40 | v2.12 |
v36-40 | v2.11 |
v33-36 | v2.10 |
v31-34 | v2.9 |
v30-33 | v2.8 |
v30-33 | v2.7 |
v29-32 | v2.6 |
v29-32 | v2.5 |
v29-32 | v2.4 |
2.3 下载对应ChromeDriver
ChromeDriver下载地址:http://chromedriver.storage.googleapis.com/index.html
我下载的chrome版本为81,对应如下图:
2.4 ChromeDriver环境配置
先打开Finder,然后进入/usr/local/bin目录(由于Mac该目录是隐藏的,所以可通过快捷键command+shift+g打开)
将解压的ChromeDriver拖拽到bin文件夹中
查看ChromeDriver版本:chromedriver --version 下面就是配置成功后输出的版本
localhost:~ wangyadong$ chromedriver --version ChromeDriver 81.0.4044.69 (6813546031a4bc83f717a2ef7cd4ac6ec1199132-refs/branch-heads/4044@{#776})
3. 编写脚本启动Chrome浏览器
from selenium import webdriver driver = webdriver.Chrome() driver.get('https://www.baidu.com/')
运行脚本,打开百度页