Windows下安装Selenium

  1. 安装python,建议在官网下载python3以上的版本
  2. 安装easy_install,找度娘
  3. 安装selenium,在命令行窗口下输入:pip install -U selenium 
  4. 下载chromedriver.exe,前往https://sites.google.com/a/chromium.org/chromedriver/downloads根据自己电脑上的Chrome版本选择版本下载,
    本机Chrome版本在Chrome右上角菜单->帮助->关于Google Chrome查看;下载完成后解压到Chrome的安装目录,默认是在
    C:\Program Files (x86)\Google\Chrome\Application
  5. 将谷歌浏览器的安装目录(默认是C:\Program Files (x86)\Google\Chrome\Application),添加到系统的用户环境变量path中
  6. 将以下测试代码复制并另存为selenium_test.py(文件存放的位置有自己决定,只是个临时测试文件)
     1 from selenium import webdriver
     2 from selenium.common.exceptions import NoSuchElementException
     3 from selenium.webdriver.common.keys import Keys
     4 import time
     5 
     6 browser = webdriver.Chrome() # Get local session of Chrome
     7 browser.get("http://www.yahoo.com") # Load page
     8 assert "Yahoo" in browser.title
     9 elem = browser.find_element_by_id("uh-search-box") # Find the query box
    10 elem.send_keys("qq" + Keys.RETURN)
    11 time.sleep(0.2) # Let the page load, will be added to the API
    在文件存放的位置使用命令行运行: python selenium_test.py
    如果有以下的错误信息:
    1 AttributeError: 'Service' object has no attribute 'process'

    请检查步骤4、5是否正确配置了,如果确定无误请使用以下办法尝试解决:
    将第四步解压出来的chromedriver.exe文件复制一份到当前测试文件的目录下,再执行测试文件。

 

posted @ 2017-12-04 20:20  TonyZhiQiang  阅读(3245)  评论(0编辑  收藏  举报