基于網頁自動化Selenium IDE的安裝與部署
- 一. Selenium IDE的簡介、安裝與使用
- 1. 什么是Selenium IDE?
- 2. Selenium IDE的安裝和使用
- 3. 匯出內容的分析
- 二. 服務器端的相關部署
- 1. python環境的部署
- 2. 安裝瀏覽器
- 3. 安裝selenium環境
- 4. 安裝selenium時可能遇到的問題
- 三. 服務器端網頁操作的回放
- 1. 自動化代碼的修改
- 2. 代碼的自動運行
一. Selenium IDE的簡介、安裝與使用
1. 什么是Selenium IDE?
SeleniumIDE是一種網頁操作的錄制和回放工具,便于熟悉Selenium WebDriver,
新的Selenium IDE旨在記錄您與網站的互動,以幫助您生成和維護站點自動化、測驗,并消除手動執行重復操作的需要,功能包括:
- 在Firefox和Chrome上錄制和播放測驗,
- 將測驗組織到套件中以便于管理,
- 保存和加載腳本,以便以后播放,
2. Selenium IDE的安裝和使用
如上文所提到的Selenium IDE可以在Firefox和Chrome上錄制和播放測驗,那么我們將以Firefox中的Selenium IDE可視化插件為例子來安裝并且錄制一個圖書館選座的操作,
-
在Firefox瀏覽器組件中加入Selenium IDE,

-
打開Selenium IDE并新建一個專案

-
鍵入需要被操作的網址,這里使用的是我的個人主頁,

-
使用Selenium IDE來錄制我們需要做的操作,此步驟中,我們可以點擊超鏈接,輸入密碼實作登錄等等功能,

-
保存和加載腳本,我們這邊以python為例,以便以后播放,


3. 匯出內容的分析
# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
class TestTest001():
def setup_method(self, method):#創建網頁容器
self.driver = webdriver.Firefox()
self.vars = {}
def teardown_method(self, method):#關閉網頁容器
self.driver.quit()
def test_test001(self):#錄制的操作
# Test name: test001
# Step # | name | target | value
# 1 | open | / |
self.driver.get("http://www.liu*****.cn/")
# 2 | setWindowSize | 789x707 |
self.driver.set_window_size(789, 707)
# 3 | click | linkText=我的愛好 |
self.driver.find_element(By.LINK_TEXT, "我的愛好").click()
# 4 | click | linkText=首 頁 |
self.driver.find_element(By.LINK_TEXT, "首 頁").click()
二. 服務器端的相關部署
1. python環境的部署
在網站python官方網站 - Welcome to Python.org中下載并且安裝Python,在這里我當時使用的是Python3.9.1版本,

2. 安裝瀏覽器
安裝Google Chrome瀏覽器
3. 安裝selenium環境
用管理員權限打開cmd并執行如下命令
pip install selenium
4. 安裝selenium時可能遇到的問題
- 使用如下安裝代碼
python -m pip install selenium - 由于網路原因selenium下載失敗導致安裝失敗:此時可以在cmd頁面中尋找
selenium-3.141.0-py2.py3-none-any檔案的下載地址https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl然后使用迅雷后者或者瀏覽器等下載工具直接將檔案下載下來,然后再用pip直接安裝這個下載好的檔案即可,
三. 服務器端網頁操作的回放
1. 自動化代碼的修改
我們將第二大步中匯出的.py檔案拷貝到服務器中,然后對其代碼進行如下修改
# Generated by Selenium IDE
#此處我洗掉了一些不必要的包的匯入
from selenium import webdriver
from selenium.webdriver.common.by import By
class TestTest001():
def setup_method(self):#創建網頁容器
self.driver = webdriver.Chrome()#創建對應瀏覽器的webdriver
self.vars = {}
def teardown_method(self):#關閉網頁容器
self.driver.quit()
def test_test001(self):#錄制的操作
# Test name: test001
# Step # | name | target | value
# 1 | open | / |
self.driver.get("http://www.liuealon.cn/")
# 2 | setWindowSize | 789x707 |
self.driver.set_window_size(789, 707)
# 3 | click | linkText=我的愛好 |
self.driver.find_element(By.LINK_TEXT, "我的愛好").click()
# 4 | click | linkText=首 頁 |
self.driver.find_element(By.LINK_TEXT, "首 頁").click()
#實體化類,并且呼叫類中的函式
YunXing = TestTest001()
YunXing.setup_method()
YunXing.test_test001()
YunXing.teardown_method()
一切正常的話我們雙擊該.py檔案,就可以運行自動化程式了,
2. 代碼的自動運行
可以通過windowsd的計劃任務來實作
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/267131.html
標籤:python
上一篇:爬取風景圖片
下一篇:CZSC 官方檔案(0.6.7)
