程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python automation test 31] mouse and keyboard operation and select usage of Web Automation

編輯:Python

List of articles

  • One 、 Preface
  • Two 、 The mouse operation
  • 3、 ... and 、 Keyboard operation
  • Four 、select usage

One 、 Preface

This article will mainly explain selenium Operation of mouse and keyboard in 、select Usage and js Simple explanation , In addition, there is a portal for a series of articles below , It's still being updated , Interested partners can also go to check , Don't talk much , Let's have a look ~

Series articles :
Series articles 1:【Python automated testing 1】 meet Python The beauty of the
Series articles 2:【Python automated testing 2】Python Installation configuration and PyCharm Basic use
Series articles 3:【Python automated testing 3】 First knowledge of data types and basic syntax
Series articles 4:【Python automated testing 4】 Summary of string knowledge
Series articles 5:【Python automated testing 5】 List and tuple knowledge summary
Series articles 6:【Python automated testing 6】 Dictionary and collective knowledge summary
Series articles 7:【Python automated testing 7】 Data operator knowledge collection
Series articles 8:【Python automated testing 8】 Explanation of process control statement
Series articles 9:【Python automated testing 9】 Function knowledge collection
Series articles 10:【Python automated testing 10】 File basic operation
Series articles 11:【Python automated testing 11】 modular 、 Package and path knowledge collection
Series articles 12:【Python automated testing 12】 Knowledge collection of exception handling mechanism
Series articles 13:【Python automated testing 13】 class 、 object 、 Collection of attribute and method knowledge
Series articles 14:【Python automated testing 14】Python Basic and advanced exercises of automatic test
Series articles 15:【Python automated testing 15】unittest The core concept and function of test framework
Series articles 16:【Python automated testing 16】 Test case data separation
Series articles 17:【Python automated testing 17】openpyxl Secondary packaging and data driven
Series articles 18:【Python automated testing 18】 Configuration file analysis and practical application
Series articles 19:【Python automated testing 19】 Log system logging Explain
Series articles 20:【Python automated testing 20】 Construction of interface automation test framework model
Series articles 21:【Python automated testing 21】 Interface automation test practice 1 _ Interface concept 、 Project introduction and test process Q & A
Series articles 22:【Python automated testing 22】 Interface automation test practice II _ Interface framework modification and use case optimization
Series articles 23:【Python automated testing 23】 Interface automation test practice III _ Dynamic parameterization and data forgery
Series articles 24:【Python automated testing 24】 Interface automation test practice IV _Python Operating the database
Series articles 25:【Python automated testing 25】 Interface automation test practice 5 _ Database assertion 、 Interface Association and related management optimization
Series articles 26:【Python automated testing 26】 Interface automation test practice 6 _pytest frame +allure Explain
Series articles 27:【Python automated testing 27】Web Automated testing theory 、 Environment construction and common operations
Series articles 28:【Python automated testing 28】html Basic grammar
Series articles 29:【Python automated testing 29】Xpath、 Axis operation and CSS Detailed explanation of element positioning
Series articles 30:【Python automated testing 30】Web The three waiting and switching of automation

Two 、 The mouse operation

Web In automation, we can operate the mouse through code control , There are many kinds of mouse operations , For example, click 、 double-click 、 Press and hold 、 hover , Drag and drop, etc , Let's briefly understand the common mouse operations :

from selenium.webdriver.common.action_chains import ActionChains
def click(driver, locator):
""" Left click operation """
el = driver.find_element(*locator)
el.click()
def double_click(driver, locator):
""" Double click with the left mouse button """
el = driver.find_element(*locator)
action = ActionChains(driver)
action.double_click(el).perform()
def context_click(driver, locator):
""" Right click operation """
el = driver.find_element(*locator)
action = ActionChains(driver)
action.context_click(el).perform()
def move_to(driver, locator):
""" Mouse over operation """
el = driver.find_element(*locator)
action = ActionChains(driver)
action.move_to_element(el).perform()
def drag_and_drop(driver, locator_start, Locator_end):
""" Mouse drag operation , Drag from the start position to the target position """
start_el = driver.find_element(*locator_start)
end_el = driver.find_element(*Locator_end)
action = ActionChains(driver)
action.move_to_element(start_el, end_el).perform()

3、 ... and 、 Keyboard operation

Keyboard operation , common Web The keyboard operation in automation is to enter text 、 Copy some spaces and return :

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
time.sleep(2)
# Enter... In the text input box CSDN
driver.find_element_by_id("kw").send_keys("CSDN")
# Call space 
driver.find_element_by_id("kw").send_keys(Keys.SPACE)
# Enter the cook... In the text input box 
driver.find_element_by_id("kw").send_keys(' The cook ')
# Future generations 
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'a')
# Copy 
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'c')
# Paste 
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'v')
# Call enter 
driver.find_element_by_id("kw").send_keys(Keys.ENTER)
time.sleep(5)
driver.close()

Four 、select usage

Sometimes the drop-down list is select object , We need to use it select Position in the same way :

from selenium.webdriver.support.select import Select
from selenium import webdriver
driver = webdriver.Chrome()
select_el = driver.find_element("xpath", "value")
# initialization Select
s = Select(select_el)
# adopt option Among the options value Property selection options 
s.select_by_value(" In the latest week ")
# Through the index 
s.select_by_index(0)
# Through the text 
s.select_by_visible_text(" In the latest week ")


All right. ~ The above is all the content shared in this article , Have you learned ? I hope I can help you !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved