python3 + selenium3 web自动化测试环境搭建

基于Python 3.5.2 + selenium 3.0.1
安装Python略,安装selenium包
pip3 install selenium
看网上各种说,总结一下selenium2自带支持firefox仅到firefox46, 47+需要mozilla官方的driver:geckodriver;selenium3必须使用geckodriver。参考范丰平的cnblog,乙醇的cnblog,百度知道。geckodriver下载地址https://github.com/mozilla/geckodriver/releases

windows下环境搭建:
win7 + Firefox49
1.将geckodriver的路径添加到系统path中,也可以查看系统path,然后找一个合适的地方将geckodriver挪过去。我采用的是后者,将geckodriver放在了Python安装路径下。
2.win+R运行firefox.exe -p,以这种方式运行Firefox所做的配置变更将会保存到Roaming…\profile中,selenium调用的是\Local…\profile,可直接将Profile文件夹复制过去,或者另行指定。
3.代码示例:

# -*- coding: utf-8 -*-
from selenium import webdriver
import time
firefox_profile = webdriver.FirefoxProfile(r'C:\Users\username\AppData\Roaming\Mozilla\Firefox\Profiles\4e3rhkho.default')
brower = webdriver.Firefox(firefox_profile=firefox_profile)
brower.maximize_window()
brower.get('https://www.baidu.com')
time.sleep(3)
pass
brower.quit()

遗留问题:quit()后会弹出Plugins Container停止工作,尚未解决,烦躁

Linux环境搭建:
Ubuntu Kylin16.04 UKUI预览版 + Firefox49
1.echo $PATH 查看path设置
2.找个合适的地方放geckodriver,比如/usr/local/bin,也可以将geckodriver的目录添加到PATH中
3.代码示例:

# -*- coding: utf-8 -*-
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://www.baidu.com')
driver.find_element_by_id('kw').send_keys('selenium2')
driver.find_element_by_id('su').click()
driver.quit()

遗留问题:以前一切OK,目前换成预览版后,driver.quit()会引起如下提示,待解决
‘NoneType’ object has no attribute ‘path’
Process finished with exit code 0

除了Mozilla官方geckodriver
MS官方 ms webdriver支持EDGE 9+
MAC官方 Safaridriver支持Safari 10+
安装和调用大同小异,未逐一实践

Q:运行代码遇到 AttributeError: ‘Service’ object has no attribute ‘process’异常
A;该异常的前文说的很明确了
FileNotFoundError: [Errno 2] No such file or directory: ‘geckodriver.exe’
During handling of the above exception, another exception occurred:
将geckodriver的目录添加到系统PATH中,或者将其放置在某个系统PATH的目录下即可

你可能感兴趣的:(python,selenium)