推荐一:
    
     《使用Python学习selenium测试工具》
    
   
    推荐二:
    
     《selenium官方文档》
    
   
- 安装python(很简单,网上很多教程)
 - 
     安装selenium
- DOS窗口输入:python -m pip install selenium
 - 
       查看安装的selenium版本号
- 打开命令行
 - 
         进入python环境,输入:
python
 - 
         导入selenium包:
import selenium
 - 
         查看版本号:
selenium.__version__
 
 
 - 
     安装chrome驱动
- 
       
下载chrome驱动
 - 解压后把chromedriver.exe文件放到chrome安装目录里
 - 把chrome安装目录的路径配置到环境变量path中
 
 - 
       
 - 
     
eclipse下的pydev安装
- 可以用其他编辑工具,pycharm等
 
 - 验证selenium是否安装成功
 
# TestSelenium.py
#!/user/bin/env python
#encoding: utf-8
from selenium import webdriver
# 此处注释的代码可能出现调起firefox不能正常请求get地址
# 这里要注意:selenium和firefox版本匹配
# 我这里装的是selenium 2.53.6 + firefox45.3
# 之前装的firefox48.0执行时会有异常,群友建议45.0以下
# 
# browser = webdriver.Firefox()
# browser.get(url="https://www.baidu.com")
# browser.close()
chrome_driver_path = r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
browser = webdriver.Chrome(chrome_driver_path)
browser.get("https://www.baidu.com")
print(browser.title)
browser.find_element_by_id("kw").send_keys("gg")
browser.find_element_by_id("su").click()
    到这里selenium基本环境已经搭建完成了。
   
- 
搭建过程中遇到个问题:
- 
未安装chrome驱动时,直接用webdriver调起firefox,使用其get方法传参时总是报错;
Traceback (most recent call last): File "E:\fish\phpWeb\eclipse\MyPydev\mysrc\test.py", line 7, in <module> browser.get("https://www.baidu.com") TypeError: get() missing 1 required positional argument: 'url' - 
webdriver文件中,关于get的定义:
def get(self, url): """ Loads a web page in the current browser session. """ self.execute(Command.GET, {'url': url}) - 
       不知道为什么错误提示需要传两个参数?另一个问题,这两个参数应该怎么传,才能保证程序正常运行?
- 经群友指点,这里定义的两个参数,其实并不是说调用时要传两个
 - 这里的self是python定义函数的一种默认形式,类似于java的this
 - 最后问题定位到:firefox浏览器版本不匹配 + 调用Firefox时,要加“()”
 
 
 - 
 
 
版权声明:本文为monkey7777原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。