关于 selenium 安装,
第一步,安装Python:
https://www.python.org/downloads 选择3.x版本(避免2.x 烦人的编码问题)
第二步,关于 SetupTools + pip(这两个工具目的是为了下载selenium),
python 2.7 + windows版本已经集成这两个工具,不用安装
如需安装,参考:http://blog.youkuaiyun.com/chinajobs/article/details/52861692
第二步,selenium 安装
pip install selenium,该命令会安装最新版本
第三步步,chrome以及 浏览器驱动安装
请选择和 浏览器 版本 匹配的chromedriver 驱动,参考 http://blog.youkuaiyun.com/wang8978/article/details/52934506
将 chromedriver.exe 所在目录加入PATH环境变量,如:D:\PATH ,注意 路径中 空格问题
测试语句:
1 from selenium import webdriver 2 from selenium.common.exceptions import TimeoutException 3 from selenium.webdriver.support.ui import WebDriverWait 4 from selenium.webdriver.support import expected_conditions as EC 5 6 7 driver = webdriver.Chrome() 8 driver.get("http://www.baidu.com") 9 10 driver.find_element_by_id("kw").send_keys("博客园") 11 driver.find_element_by_id("su").click() 12 13 try: 14 # we have to wait for the page to refresh, the last thing that seems to be updated is the title 15 WebDriverWait(driver, 5).until(EC.title_contains("博客园")) 16 17 # You should see "博客园_百度搜索" 18 print(driver.title) # Python 3 中语法:print("display something ...") 19 20 finally: 21 driver.quit()