python + selenium_ keyboard events

introduction

---- In the actual web testing work, you need to cooperate with the keyboard keys to operate. The keys () class of webdriver provides the operation of all the keys on the keyboard, and can also simulate the combination keys Ctrl + a, Ctrl + v, etc.

 

Examples:

# cording = gbk 
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By #import by method
from selenium.webdriver.common.action_chains import ActionChains ## Action on mouse events
from selenium.webdriver.common.keys import Keys # Operate on keyboard events

current_path = os.path.dirname (__ file__) firefox_path
= current_path + "/../ webdriver / geckodriver.exe"
driver = webdriver.Firefox (executable_path =
firefox_path ) driver.get ("http: // www.baidu.com ")

# First enter Baidu
driver.find_element_by_id ('kw'). send_keys ('Baidu')
time.sleep (3)
# 1. Delete degree
driver.find_element_by_id ('kw'). send_keys (Keys. BACK_SPACE)
time.sleep (3)

# 2. Clear the input box and re-enter the value
driver.find_element_by_id ('kw'). clear ()
driver.find_element_by_id ('kw'). send_keys ('Angel')
time.sleep (5)

# 3.ctrl + a Select all content in the input box
driver.find_element_by_id ( 'kw'). send_keys (Keys.CONTROL, 'a')
time.sleep (3)

# 4.ctrl + x Cut the content in the input box
driver.find_element_by_id ('kw'). send_keys (Keys.CONTROL, ' x ')
time.sleep (3)

# 5. ctrl + v paste the cut content
driver.find_element_by_id (' kw '). send_keys (Keys.CONTROL,' v ')
time.sleep (3)

# 6. Enter
driver.find_element_by_id ('su'). send_keys (Keys.ENTER)
time.sleep (3)

Guess you like

Origin www.cnblogs.com/123anqier-blog/p/12729482.html