Web automated testing seven ----- mouse, keyboard

First, the mouse

In webdriver, some operations, such as a mouse: Double, right-click, hover, drag and so is encapsulated in ActionChains class, we only need to use when introducing the class on it.

Mouse class provides a common method 0.ActionChains:

  • perform (): execution behavior of all ActionChains stored.
  • context_click (): Right-click
  • double_click (): double-click
  • drag_and_drop (): Drag
  • move_to_element (): Hover

note:

  • Need to introduce ActionChains class before use.
from selenium.webdriver.common.action_chains import ActionChains

Right-click examples

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains  # 引入 ActionChains 类 browser = webdriver.Chrome() browser.get('https://www.baidu.com') # 定位到要右击的元素 right_click = browser.find_element_by_link_text('新闻') # 对定位到的元素执行鼠标右键操作 #ActionChains(driver):调用ActionChains()类,并将浏览器驱动browser作为参数传入 #context_click(right_click):模拟鼠标双击,需要传入指定元素定位作为参数 #perform():执行ActionChains()中储存的所有操作,可以看做是执行之前一系列的操作 try: ActionChains(browser).context_click(right_click).perform() print('成功右击') except Exception as e: print('fail') #输出内容:成功双击

note:

  • ActionChains (driver): call ActionChains () class, browser and browser driver passed as a parameter
  • context_click (right_click): Analog and double click need to pass as a parameter to specify the positioning element
  • perform (): execution ActionChains () stored in all operations, can be seen as a series of operations performed before

1. Right-click

  • context_click (): Right-click
#   鼠标右击
# 定位到要右击的元素
right_click  = browser.find_element_by_id("xx")
 # 对定位到的元素执行右击操作 ActionChains(browser).move_to_element(right_click ).perform()

2. Double-click the Mouse

  • double_click (): double-click
# 定位到要右击的元素
double_click = browser.find_element_by_id('xx')

# 对定位到的元素执行鼠标右键操作
ActionChains(browser).context_click(double_click).perform()

3. Drag the mouse

  • drag_and_drop(source,target):拖动
  • source: starting position; need to drag the elements
  • target: end position; Drag need to place a destination element
# 开始位置:定位到元素的原位置
source = driver.find_element_by_id("xx")

# 结束位置:定位到元素要移动到的目标位置
target = driver.find_element_by_id("xx")

# 执行元素的拖放操作 ActionChains(driver).drag_and_drop(source,target).perform()

4. Hover

  • move_to_element (): Hover
# 定位到要悬停的元素
move = driver.find_element_by_id("xx")

# 对定位到的元素执行悬停操作
ActionChains(driver).move_to_element(move).perform()

  

Second, keyboard

Commonly used keyboard:

  • send_keys (Keys.BACK_SPACE): Delete key (BackSpace)
  • send_keys (Keys.SPACE): spacebar (Space)
  • send_keys (Keys.TAB): Tabulator (TAB)
  • send_keys(Keys.ESCAPE):回退键(ESCAPE)
  • send_keys (Keys.ENTER): Enter key (ENTER)
  • send_keys(Keys.CONTROL,'a'):全选(Ctrl+A)
  • send_keys(Keys.CONTROL,'c'):复制(Ctrl+C)
  • send_keys (Keys.CONTROL, 'x'): Cut (Ctrl + X)
  • send_keys (Keys.CONTROL, 'v'): Paste (Ctrl + V)
  • send_keys (Keys.F1): Keyboard F1
  • .....
  • send_keys (Keys.F12): F12 keyboard
import time
from selenium.webdriver import Chrome, ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC

driver = Chrome()

driver.get('http://www.baidu.com')

def wait_click_element(driver, locator):
    wait = WebDriverWait(driver, 20)
    return wait.until(EC.element_to_be_clickable(locator))

e = driver.find_element_by_id('kw')

e.send_keys('ningmengban')
e.send_keys(Keys.CONTROL, 'a')

time.sleep(2)

 

Guess you like

Origin www.cnblogs.com/qyh0902/p/11223273.html