1 安裝python
下載Python 3.6
注:如果手裏有有python3.6的安裝包,則跳過下載Python 3.6的步驟。
下載python3.6.x,官網下載地址是:https://www.python.org/downloads/,選擇合適的版本,點擊download,進入下載頁面
在下載頁面選擇executable installer格式的文件,點擊即可下載:
安裝Python
雙擊下載好的python-3.6.4.exe,運行下載的EXE安裝包,一定要勾選上Add Python 3.6 to Path,不然用起來特別不方便。然後點擊Install Now進行安裝。
運行python
安裝成功後,打開CMD命令提示符窗口,輸入python後,如果出現類似如下內容,則python安裝成功;如果沒有出現,極有可能是因爲安裝過程中沒有勾選Add Python 3.6 to Path。請參考網上python添加環境變量的教程。
打開CMD窗口,輸入:
pip3 install selenium
會自動安裝selenium,如無報錯,且提示安裝成功,則成功安裝selenium。
我們可以驗證是否安裝成功並且能正常使用,驗證方式:
打開python自帶的IELD,輸入from selenium importwebdriver,敲擊回車。如果沒有報錯,則selenium成功安裝且能正常使用。
基本環境安裝好了,我們需要一個好用的IDE進行腳本的編寫工作。個人推薦pycharm,也可以根據自己的喜好進行選擇。
進入https://www.jetbrains.com/pycharm/download/#section=windows 下載pycharm。Professional是專業版,功能強大,但需要收費
Community是社區版,一般來說功能夠用,而且免費
可以根據個人需求來選擇。
具體安裝步驟和使用方法,可以參考網上資料。
4 安裝firefox(火狐)瀏覽器和相關插件
爲什麼需要安裝firefox瀏覽器呢,原因是firefox瀏覽器有兩個插件是我們需要用到的:
Selenium IDE:可以實現腳本錄製
Firebug:幫助我們定位元素
4.1 安裝firefox
首先我們下載firefox,因爲Selenium IDE不能支持最新版本的firefox瀏覽器,所以我們選擇版本爲54的firefox,下載鏈接如下:
1. http://ftp.mozilla.org/pub/firefox/releases/54.0/win64/zh-CN/
2.火狐瀏覽器多個版本下載鏈接(很重要): http://ftp.mozilla.org/pub/firefox/releases/
安裝過程省略
然後打開firefox瀏覽器,點擊,然後點擊進入設置頁面
依次點擊高級>>更新>>不檢查更新,爲了防止firfox自動更新後不支持selenium IDE。
4.2 安裝Selenium IDE插件
用firefox瀏覽器打開鏈接:
https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/
出現以下畫面,點擊+ Add to Firefox即可添加該插件,安裝好後,重啓瀏覽器生效。
重啓瀏覽器後,打開Selenium IDE,出現以下界面,則成功添加該插件:
4.3 添加Firebug插件
用firefox瀏覽器打開鏈接:
https://addons.mozilla.org/zh-CN/firefox/addon/firebug/
出現以下畫面,點擊+ Add to Firefox即可添加該插件,安裝好後,重啓瀏覽器生效。
重啓瀏覽器後,鍵盤敲擊F12,得出如下界面,則添加插件成功:
5 安裝Firefox驅動
以上環境都裝好了,我們需要思考這樣一個問題。Python和selenium怎麼去操作瀏覽器的呢?
我們需要安裝瀏覽器的driver(驅動),不同的瀏覽器有不同的驅動,對於firefox,我們需要打開以下鏈接:
https://github.com/mozilla/geckodriver/releases
選擇合適的版本,因爲我是64位的windows系統,所以我選擇下載geckodriver-v0.19.1-win64.zip
下載完成後將geckodriver-v0.19.1-win64.zip解壓到python的根錄下。這裏我的python安裝路徑在E:\Python\Python36-32\Scripts,所以我解壓到E:\Python\Python36-32\Scripts下面。
6 測試整個環境以及代碼
運行如下代碼,如果能成功打開聽旅系統頁面,則環境搭建完成:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import Select from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import NoAlertPresentException import unittest, time, re class Pyauto0808(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() self.driver.implicitly_wait(5) self.base_url = "https://www.imguider.com/manager/index.html" self.verificationErrors = [] self.accept_next_alert = True def test_pyauto0808(self): driver = self.driver driver.get(self.base_url + "/manager/index.html") driver.find_element_by_name("username").clear() driver.find_element_by_name("username").send_keys("erha") driver.find_element_by_name("password").clear() driver.find_element_by_name("password").send_keys("erha123") driver.find_element_by_id("login").click() driver.find_element_by_css_selector("span.title").click() driver.find_element_by_css_selector("li.orders > a > span.title").click() driver.find_element_by_link_text("2").click() driver.find_element_by_link_text("3").click() driver.find_element_by_xpath("//div[@id='left']/div/ul/li[8]/a/span").click() driver.find_element_by_link_text(u"優惠券管理").click() driver.find_element_by_id("add-coupon2").click() driver.find_element_by_css_selector("input.layui-layer-input").clear() driver.find_element_by_css_selector("input.layui-layer-input").send_keys("1") driver.find_element_by_link_text(u"確定").click() driver.find_element_by_css_selector("input.layui-layer-input").clear() driver.find_element_by_css_selector("input.layui-layer-input").send_keys("0808") driver.find_element_by_link_text(u"何晨").click() driver.find_element_by_link_text(u"退出登錄").click() def is_element_present(self, how, what): try: self.driver.find_element(by=how, value=what) except NoSuchElementException as e: return False return True def is_alert_present(self): try: self.driver.switch_to_alert() except NoAlertPresentException as e: return False return True def close_alert_and_get_its_text(self): try: alert = self.driver.switch_to_alert() alert_text = alert.text if self.accept_next_alert: alert.accept() else: alert.dismiss() return alert_text finally: self.accept_next_alert = True def tearDown(self): self.driver.quit() self.assertEqual([], self.verificationErrors) if __name__ == "__main__": unittest.main()