python+selenium+firefox使用例子windows环境-泓源视野

python+selenium+firefox使用例子windows环境

关于selenium
selenium是一款强大的web自动化测试工具,它通过webdriver与浏览器进行通信,从而与web进行交互。
实验环境
win7 64位、python 3.6.4、selenium 3.13.0、firefox 49.0.2
环境部署
1、安装selenium
pip install selenium
1
2、安装driver for firefox
请按浏览器选择对应的driver下载,我这里是firefox的。
http://selenium-python.readthedocs.io/installation.html#drivers
firefox driver下载版本地址:
https://github.com/mozilla/geckodriver/releases
下载下来之后,放到任意目录,然后,将geckodriver.exe所在路径加到系统环境变量PATH中(修改后可能需要注销才会生效,命令行下echo %PATH%可查看有没有生效)
使用例子
from selenium import webdriver
#如果firefox没有安装在默认位置,就要手动指定位置
location = 'D:/Program Files/Mozilla Firefox/firefox.exe'
driver = webdriver.Firefox(firefox_binary=location)
#请求页面
driver.get("https://www.xiaoying.com/user/login")
#通过css选择器获取相应元素
jUsername = driver.find_element_by_css_selector('.jUsername')
#填写前先清空
jUsername.clear()
#模拟系统按键
jUsername.send_keys("18812345678")
jPassword = driver.find_element_by_css_selector('.jPassword')
jPassword.clear()
jPassword.send_keys("pass123456")
jSubmit = driver.find_element_by_css_selector('.jSubmit')
#模拟点击登录按钮(这里帐号密码是乱填的,所以是登录不成功的)
jSubmit.click()
driver.close()
注意:
报Message: Unable to find a matching set of capabilities,是因为浏览器版本与driver版本不兼容,这时需要升降级你的浏览器/driver的版本,让他们匹配上;比如我的firefox 49,与driver-v0.15.0兼容;
报Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line,这是因为找不到浏览器执行文件路径,安装在非默认路径,就会报这个错,只要在创建webdriver时将浏览器执行文件路径通过参数传进去即可;
————————————————
本文由 泓源视野 作者:admin 发表,其版权均为 泓源视野 所有,文章内容系作者个人观点,不代表 泓源视野 对观点赞同或支持。如需转载,请注明文章来源。
18

发表评论

Protected with IP Blacklist CloudIP Blacklist Cloud
您是第8237361 位访客, 您的IP是:[18.206.14.46]