一、安裝Python環境及IDE
1、詳情 查看如下文章:https://blog.csdn.net/Eayonz/article/details/106469500
(1)更新Python pip,以及展示selenium版本指令如下:
python -m pip install --upgrade pip
pip show selenium(我這邊的Firefox已經裝了ide插件,若使用指令pip show selenium不能展示版本號,請查看網上Python pip安裝selenium的方法步驟或者尋找其他方法)
二、下載瀏覽器驅動及環境配置
1、這裏以火狐瀏覽器驅動爲例,地址如下:
下載地址:https://github.com/mozilla/geckodriver/releases
(1)下載完成後解壓即可使用
2、將瀏覽器驅動放置Python目錄下的Scripts文件下內,具體路徑如下:
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\Scripts(此處的Python是默認路徑安裝的)
(1)如有需要,請將webDriver驅動存放路徑,加入到path環境變量,webDriver 驅動應與瀏覽器版本相符。
三、創建項目寫入代碼並執行
(1)可以用selenium ide直接錄製腳本並導出腳本,將腳本導入到pycharm中(此處錄製的腳本適用於python的單元測試模塊unittest)。
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 TestTestss():
def setup_method(self, method):
self.driver = webdriver.Firefox()
self.vars = {}
def teardown_method(self, method):
self.driver.quit()
def test_testss(self):
self.driver.get("http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
self.driver.set_window_size(1889, 980)
self.driver.find_element(By.CSS_SELECTOR, "#account .ivu-input").click()
self.driver.find_element(By.CSS_SELECTOR, ".ivu-input:nth-child(3)").send_keys("cc")
self.driver.find_element(By.CSS_SELECTOR, ".ivu-input:nth-child(2)").click()
self.driver.find_element(By.CSS_SELECTOR, "#psw .ivu-input").send_keys("123456")
self.driver.find_element(By.CSS_SELECTOR, ".ivu-btn").click()
element = self.driver.find_element(By.CSS_SELECTOR, ".ivu-btn")
actions = ActionChains(self.driver)
actions.move_to_element(element).perform()
element = self.driver.find_element(By.CSS_SELECTOR, "body")
actions = ActionChains(self.driver)
actions.move_to_element(element, 0, 0).perform()
self.driver.find_element(By.CSS_SELECTOR, ".center:nth-child(9) > .title > div:nth-child(1)").click()
self.driver.find_element(By.CSS_SELECTOR, ".el-table__fixed-body-wrapper .el-table__row:nth-child(1) .el-button:nth-child(1) > span").click()
self.driver.find_element(By.CSS_SELECTOR, "textarea").click()
self.driver.find_element(By.CSS_SELECTOR, "textarea").send_keys("111111")
self.driver.find_element(By.CSS_SELECTOR, ".button").click()
(2)以下代碼適用於簡單的python file,操作如下:登錄網站,輸入賬號密碼,選擇反饋管理按鈕,選擇回覆,輸入信息併發送給反饋的人。循環4次。
import pytest
import time
import json
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
from selenium import webdriver # 引入包
i = 1
while i < 5:
driver = webdriver.Firefox() # 實例化瀏覽器
driver.get("http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") # 打開網頁
driver.find_element(By.CSS_SELECTOR, "#account .ivu-input").click()
driver.find_element(By.CSS_SELECTOR, "#account .ivu-input").send_keys("cc")
driver.find_element(By.CSS_SELECTOR, ".ivu-input:nth-child(2)").click()
driver.find_element(By.CSS_SELECTOR, "#psw .ivu-input").send_keys("123456")
driver.find_element(By.CSS_SELECTOR, ".ivu-btn").click()# 點擊登錄按鈕
time.sleep(2)#強制停留2s
driver.find_element(By.CSS_SELECTOR, ".center:nth-child(9) > .title > div:nth-child(1)").click()
time.sleep(2)#強制停留2s
driver.find_element(By.CSS_SELECTOR,
".el-table__fixed-body-wrapper .el-table__row:nth-child(1) .el-button:nth-child(1) > span").click()
driver.find_element(By.CSS_SELECTOR, "textarea").click()
driver.find_element(By.CSS_SELECTOR, "textarea").send_keys("我在測試循環第%d次"%i)
driver.find_element(By.CSS_SELECTOR, ".button").click()
driver.save_screenshot('D:/a.png')#截圖存放至D盤
driver.quit() # 關閉瀏覽器
i += 1
(3)一些元素定位的api:
id定位 driver.find_element_by_id("xx")
name定位 driver.find_element_by_name("xx")
class定位 driver.find_element_by_class_name("xx")
tag定位 driver.find_element_by_tag_name("xx")
link定位 driver.find_element_by_link_text("xx").click()
(4)截屏操作
driver.save_screenshot('D:/a.png')#截圖存放至D盤