• web自动化测试(5)--鼠标、键盘操作


    一、鼠标操作

    常见的鼠标操作有:

    (1)click():单击

    (2)double_click():双击

    (3)context_click():右击

    (4)click_and_hold():按下鼠标左键不松开

    (5)release():鼠标左键抬起操作

    (6)move_to_element():鼠标悬浮在元素上

    (7)drag_and_drop():拖动

    (8)perform():提交鼠标操作的内容

    在执行鼠标操作前,需要导入提供鼠标操作的ActionChains类:from selenium.webdriver import ActionChains

    主要操作流程为:

    1、存储鼠标操作;

    2、使用perform()执行鼠标操作。

    【实例】百度首页的设置操作

    方式一:使用ActionChains类

     1 from selenium import webdriver
     2 from selenium.webdriver.common.by import By
     3 from selenium.webdriver.support.wait import WebDriverWait
     4 from selenium.webdriver.support import expected_conditions as EC
     5 from selenium.webdriver.common.action_chains import ActionChains
     6 
     7 driver = webdriver.Chrome()
     8 driver.get("http://www.baidu.com")
     9 
    10 wait = WebDriverWait(driver,20)
    11 
    12 setting_loc = (By.ID,'s-usersetting-top')
    13 wait.until(EC.visibility_of_element_located(setting_loc))
    14 ele = driver.find_element(*setting_loc)
    15 
    16 # 启动鼠标操作
    17 ac = ActionChains(driver)
    18 ac.move_to_element(ele)  # 鼠标移动到设置元素上
    19 # 执行鼠标操作
    20 ac.perform()
    21 
    22 search_loc = (By.XPATH,'//a[text()="高级搜索"]')
    23 wait.until(EC.visibility_of_element_located(search_loc))
    24 driver.find_element(*search_loc).click()   # 点击高级搜索按钮

    方式二:不使用ActionChains类

     1 from selenium import webdriver
     2 from selenium.webdriver.common.by import By
     3 from selenium.webdriver.support.wait import WebDriverWait
     4 from selenium.webdriver.support import expected_conditions as EC
     5 
     6 driver = webdriver.Chrome()
     7 driver.get("http://www.baidu.com")
     8 
     9 wait = WebDriverWait(driver,20)
    10 
    11 setting_loc = (By.ID,'s-usersetting-top')
    12 wait.until(EC.visibility_of_element_located(setting_loc))
    13 driver.find_element(*setting_loc).click()
    14 
    15 search_loc = (By.XPATH,'//a[text()="高级搜索"]')
    16 wait.until(EC.visibility_of_element_located(search_loc))
    17 driver.find_element(*search_loc).click()   # 点击高级搜索按钮

    二、键盘操作

    在自动化测试中,我们不可避免的会使用到回车、crtl+c复制等操作,这些都离不开键盘,接下来我们将介绍键盘的操作。

    在执行键盘操作前,需要导入提供键盘操作的Keys类:from selenium.webdriver.common.keys import Keys

    常见的键盘操作有:

    (1)ENTER

    (2)TAB

    (3)CLEAR

    (4)CANCEL

    ......

    【实例】百度首页输入”selenium“,按回车进行搜索

     1 from selenium import webdriver
     2 from selenium.webdriver.common.by import By
     3 from selenium.webdriver.support.wait import WebDriverWait
     4 from selenium.webdriver.support import expected_conditions as EC
     5 from selenium.webdriver.common.keys import Keys
     6 
     7 driver = webdriver.Chrome()
     8 driver.get("http://www.baidu.com")
     9 
    10 wait = WebDriverWait(driver,20)
    11 
    12 input_loc = (By.ID,"kw")
    13 wait.until(EC.visibility_of_element_located(input_loc))
    14 driver.find_element(*input_loc).send_keys("selenium",Keys.ENTER)

    补充说明:

    如何使用ctri+c键?

    send_keys(Keys.CONTROL,"c")

  • 相关阅读:
    20170728xlVba SSC_TODAY
    卸载angular版本
    union 共用体
    bootstrap的粗认识
    结构体,结构体数组,结构体指针
    C语言的枚举
    nodeJS 的认识
    nodejs 平台搭建
    动态表单
    指针
  • 原文地址:https://www.cnblogs.com/yif930916/p/14794628.html
Copyright © 2020-2023  润新知