写在最前面:
搞自动化测试呢有很大概率会用到selenium,其实用什么语言都可以,这次先从python讲起。其实在win下我已经用了很久了,今天讲讲Mac下的安装。
首先是环境搭建,本文基于macOS Mojave操作系统,我用的是python3.6+pycharm,这个就不介绍了,然后我们来安装selenium。
pip3 install selenium
Collecting selenium
Downloading https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904kB)
100% |████████████████████████████████| 911kB 11kB/s
Requirement already satisfied: urllib3 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from selenium) (1.23)
Installing collected packages: selenium
Successfully installed selenium-3.141.0
然后下载一个chromedriver,链接如下http://npm.taobao.org/mirrors/chromedriver/,自己挑选合适的对应的chrome的版本即可。
我的chrome版本是
我下的chromedriver版本是71.0.3578.33/
我按照win的习惯放在项目目录下了。
接下来简单测试一下:
from selenium import webdriver
import selenium
if __name__ == "__main__":
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
不出意外的报错了
Traceback (most recent call last):
File "/Users/Arithmetic/PycharmProjects/AutoTest/FirstTest.py", line 5, in <module>
driver = webdriver.Chrome()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
添加chromedriver的路径:
sudo vim .bash_profile
输入密码
export PATH=$PATH:ChromeDriver
esc
:
wq!
source .bash_profile
然后又报了另一个错
Traceback (most recent call last):
File "/Users/Arithmetic/PycharmProjects/AutoTest/FirstTest.py", line 5, in <module>
driver = webdriver.Chrome()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
stdin=PIPE)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1344, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
NotADirectoryError: [Errno 20] Not a directory: 'chromedriver'
尝试将chromedriver放入
/usr/local/bin
成功!
其实我试过了,不添加路径也可以。。。
记得输入完整的网址,不然貌似打不开。
今天只讲环境搭建。
今天双十一实际上只剁手了一件nike的羽绒服,原价1199,实付401,真**便宜,晚安各位~