四 Selenium4.0+Python3系列 - 常見元素操作(含鼠標鍵盤事件)

一、寫在前面上篇文章介紹的是關于瀏覽器的常見操作 , 接下來,我們將繼續分享關于元素的常見操作,建議收藏、轉發!
二、元素的狀態在操作元素之前,我們需要了解元素的常見狀態 。
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、元素點擊操作演示案例:
四 Selenium4.0+Python3系列 - 常見元素操作(含鼠標鍵盤事件)

文章插圖
點擊(鼠標左鍵)頁面按鈕: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操作演示案例:
四 Selenium4.0+Python3系列 - 常見元素操作(含鼠標鍵盤事件)

文章插圖
點擊(鼠標左鍵)頁面按鈕: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、輸入、清空輸入操作演示案例:
四 Selenium4.0+Python3系列 - 常見元素操作(含鼠標鍵盤事件)

文章插圖
輸入、清空輸入操作: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、常見鼠標操作演示案例:
四 Selenium4.0+Python3系列 - 常見元素操作(含鼠標鍵盤事件)

文章插圖
常見鼠標操作很多 , 如左鍵點擊、懸浮、移動、雙擊、右鍵等等 , 示例代碼如下:
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到F12send_keys(Keys.F1) 把F1改成對應的快捷鍵復制Ctrl+Csend_keys(Keys.CONTROL,'c')粘貼Ctrl+Vsend_keys(Keys.CONTROL,'v')全選Ctrl+Asend_keys(Keys.CONTROL,'a')剪切Ctrl+Xsend_keys(Keys.CONTROL,'x')制表鍵Tabsend_keys(Keys.TAB)五、演示案例源碼示例代碼:
【四 Selenium4.0+Python3系列 - 常見元素操作(含鼠標鍵盤事件)】# -*- coding: utf-8 -*-"""@Time : 2022/10/25 21:39@Auth : 軟件測試君@File :element_actions.py@IDE :PyCharm@Motto:ABC(Always Be Coding)"""import timefrom selenium.webdriver import Keys, ActionChainsfrom selenium.webdriver.common.by import Byfrom selenium import webdriverfrom selenium.webdriver.chrome.service import Servicefrom 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()

推薦閱讀