安装python和pip上述文章有介绍,在这里不在赘述。直接安装seleinum
pip3 install selenium
安装完成以后,
pip3 show selenium
显示
Name: selenium
Version: 3.4.3
Summary: Python bindings for Selenium
Home-page: https://github.com/SeleniumHQ/selenium/
Author: UNKNOWN
Author-email: UNKNOWN
License: Apache 2.0
Location: c:\program files\python35\lib\site-packages
Requires:
证明安装成功:
现在来测试一个,编写一个文件baidu.py
#coding=utf-8
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.baidu.com")
driver.find_element_by_id("kw").sed_keys("Selenium2")
driver.find_element_by_id("su").click()
driver.quit()
保存完以后,执行python baidu.py
报错:
File "/usr/local/lib/python2.7/dist-packages/selenium-3.0.0b2-py2.7.egg/selenium/webdriver/firefox/webdriver.py", line 65, in __init__
self.service.start()
File "/usr/local/lib/python2.7/dist-packages/selenium-3.0.0b2-py2.7.egg/selenium/webdriver/common/service.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: ‘geckodriver‘ executable needs to be in PATH.
这个时候,需要去下载geckodriver.exe
下载地址:
https://github.com/mozilla/geckodriver/releases
请根据系统版本选择下载;(如Windows 64位系统)
下载解压后将getckodriver.exe复制到Firefox的安装目录下,如(C:\Program Files\Mozilla Firefox),并在环境变量Path中添加路径:C:\Program Files\Mozilla Firefox;
如果运行,还报错
selenium.common.exceptions.WebDriverException: Message: Unable to find a matching set of capabilities
这个说明,geckodriver.exe和firefox的版本不一致所致,需要找到对应的版本即可。
ok,不早了,晚安啦!