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文件復制一份到當前測試文件的目錄下,再執行測試文件。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM