一、寫在前面
上篇文章介紹的是關于浏覽器的常見操作,接下來,我們将繼續分享關于元素的常見操作,建議「收藏、轉發!」
二、元素的狀态
在操作元素之前,我們需要了解元素的常見狀态。
1、常見元素狀态判斷,傻傻分不清
- is_displayed()
- is_enabled()
- is_selected()
2、is_displayed()
判斷元素是否顯示
element.is_displayed()
「注意:」
判斷button是否顯示,和is_displayed()容易混淆的是is_enabled()。
差別在于,直接用element.is_enabled()方法判斷button是否顯示,傳回值為true,因為button是使用CSS方法判斷是否有效,這并不是真正的方法,需要判斷其class中是否有值為disabled來判斷是否真正處于disabled的狀态.
3、is_enabled()
判斷元素是否有效,即是否為灰化狀态
element.is_enabled()
4、is_selected()
一般判斷表單元素,如radio或checkbox是否被選中。
element.is_selected()
三、常見元素的操作
這部分主要示範的常見點選操作,例如:文本輸入、複選框、單選按鈕、選擇選項、滑鼠點選事件等等。
1、元素點選操作
「示範案例:」
「點選(滑鼠左鍵)頁面按鈕」:click()
「示例代碼如下:」
driver.get("http://localhost:8080/click.html")
button1 = driver.find_element(By.ID, "button1")
is_displayed = button1.is_enabled()
if is_displayed:
button1.click()
2、Submit操作
「示範案例:」
「點選(滑鼠左鍵)頁面按鈕」:submit()
「示例代碼如下:」
driver.get("http://localhost:8080/submit.html")
login = driver.find_element(By.ID, "login")
is_displayed = login.is_enabled()
if is_displayed:
login.submit()
# login.click()
「小貼士:」
支援submit的肯定支援click,但是支援click的,不一定支援submit,可能會報錯如下:
3、輸入、清空輸入操作
「示範案例:」
「輸入、清空輸入操作」:clear(), send_keys()
「示例代碼如下:」
username = driver.find_element(By.CSS_SELECTOR, "input[type='text']")
username.clear()
username.send_keys(u"公衆号:軟體測試君")
# 輸出:公衆号:軟體測試君
print('輸入值:{0}'.format(username.get_attribute("value")))
time.sleep(1)
四、滑鼠鍵盤事件操作
1、模拟回車操作
模拟打開百度搜尋輸入部落格園,回車操作,示例代碼如下:
driver.get("https://www.baidu.com/")
driver.find_element(By.ID, "kw").send_keys("久曲健 部落格園", Keys.ENTER)
2、常見滑鼠操作
「示範案例:」
常見滑鼠操作很多,如左鍵點選、懸浮、移動、輕按兩下、右鍵等等,示例代碼如下:
driver.get("http://localhost:8080/mouse.html")
# 滑鼠左鍵點選
ActionChains(driver).click(driver.find_element(By.ID, "mouse2")).perform()
time.sleep(1)
driver.switch_to.alert.accept()
time.sleep(1)
# 滑鼠懸浮并移動操作
ActionChains(driver).move_to_element(driver.find_element(By.ID, "mouse1")).pause(1).move_to_element(
driver.find_element(By.ID, "mouse6")).perform()
time.sleep(1)
driver.switch_to.alert.accept()
# 滑鼠輕按兩下操作
ActionChains(driver).double_click(driver.find_element(By.ID, "mouse3")).perform()
time.sleep(1)
driver.switch_to.alert.accept()
# 滑鼠右鍵
ActionChains(driver).context_click(driver.find_element(By.ID, "mouse5")).perform()
3、常見的鍵盤操作
鍵盤操作 對應代碼 鍵盤F1到F12 send_keys(Keys.F1) 把F1改成對應的快捷鍵 複制Ctrl+C send_keys(Keys.CONTROL,'c') 粘貼Ctrl+V send_keys(Keys.CONTROL,'v') 全選Ctrl+A send_keys(Keys.CONTROL,'a') 剪切Ctrl+X send_keys(Keys.CONTROL,'x') 制表鍵Tab send_keys(Keys.TAB)
五、示範案例源碼
「示例代碼:」
# -*- coding: utf-8 -*-
"""
@Time : 2022/10/25 21:39
@Auth : 軟體測試君
@File :element_actions.py
@IDE :PyCharm
@Motto:ABC(Always Be Coding)
"""
import time
from selenium.webdriver import Keys, ActionChains
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
'''
初始化操作
'''
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
def init():
# 最大化操作
driver.maximize_window()
driver.set_script_timeout(60)
# 智能等待找到元素後立即繼續執行,全局生效
driver.implicitly_wait(60)
driver.set_page_load_timeout(60)
init()
'''
元素點選操作
'''
def clickDemo():
# 點選(滑鼠左鍵)頁面按鈕:click()
driver.get("http://localhost:8080/click.html")
button1 = driver.find_element(By.ID, "button1")
is_displayed = button1.is_enabled()
if is_displayed:
button1.click()
# 關閉彈窗
driver.switch_to.alert.accept()
### 元素基本操作
clickDemo()
time.sleep(1)
'''
submit操作
'''
def submitDemo():
# 點選(滑鼠左鍵)頁面按鈕:submit()
driver.get("http://localhost:8080/submit.html")
login = driver.find_element(By.ID, "login")
is_displayed = login.is_enabled()
if is_displayed:
login.submit()
# login.click()
# 小貼士:支援submit的肯定支援click,但是支援click的,不一定支援submit,可能會報錯如下:
submitDemo()
'''
輸入、清空輸入操作
'''
def clearInputDemo():
# 輸入、清空輸入操作:clear() send_keys()
username = driver.find_element(By.CSS_SELECTOR, "input[type='text']")
username.clear()
username.send_keys(u"公衆号:軟體測試君")
# 輸出:公衆号:軟體測試君
print('輸入值:{0}'.format(username.get_attribute("value")))
time.sleep(1)
clearInputDemo()
'''
模拟打開百度搜尋輸入部落格園,回車操作
'''
def mockEnterDemo():
# 模拟打開百度搜尋輸入部落格園,回車操作 示例代碼
driver.get("https://www.baidu.com/")
driver.find_element(By.ID, "kw").send_keys("久曲健 部落格園", Keys.ENTER)
### 鍵盤操作
mockEnterDemo()
def mouseDemo():
driver.get("http://localhost:8080/mouse.html")
# 滑鼠左鍵點選
ActionChains(driver).click(driver.find_element(By.ID, "mouse2")).perform()
time.sleep(1)
driver.switch_to.alert.accept()
time.sleep(1)
# 滑鼠懸浮并移動操作
ActionChains(driver).move_to_element(driver.find_element(By.ID, "mouse1")).pause(1).move_to_element(
driver.find_element(By.ID, "mouse6")).perform()
time.sleep(1)
driver.switch_to.alert.accept()
# 滑鼠輕按兩下操作
ActionChains(driver).double_click(driver.find_element(By.ID, "mouse3")).perform()
time.sleep(1)
driver.switch_to.alert.accept()
# 滑鼠右鍵
ActionChains(driver).context_click(driver.find_element(By.ID, "mouse5")).perform()
### 常見鍵盤事件操作
mouseDemo()
time.sleep(3)
driver.quit()
六、最後
到此,常見元素操作示範結束,這裡隻是列舉了一些常用的操作,關于其他操作,感興趣的同學請左鍵檢視源代碼 !
我是六哥,如果覺得文章對您有幫助,建議收藏、轉發!
請繼續關注我,我的公衆号:軟體測試君,并幫忙轉發文章到朋友圈。
你的每一次轉發,我都當做了喜歡!