平常我们会用到很多快捷键,那么selenium如何来模拟快捷键操作呢。下面看看下常用快捷键的操作。首先使用快捷键需要导入以下这个包
from selenium.webdriver.common.keys import Keys
然后在通过Keys去调用一些方法,如:
全选 send_keys(Keys.CONTROL,'a')
复制 send_keys(Keys.CONTROL,'c')
粘贴 send_keys(Keys.CONTROL,'v')
简单demo如下:
from selenium.webdriver.common.keys import Keys from selenium import webdriver import time driver = webdriver.Chrome() driver.maximize_window() driver.get('http://106.13w/indeogin') driver.find_element_by_css_selector('#login-email-address').send_keys('admin') time.sleep(1) # todo 全选 driver.find_element_by_css_selector('#login-email-address').send_keys(Keys.CONTROL,'a') time.sleep(2) # TODO 复制 driver.find_element_by_css_selector('#login-email-address').send_keys(Keys.CONTROL,'c') time.sleep(2) # todo 粘贴 driver.find_element_by_css_selector('#login-password').send_keys(Keys.CONTROL,'v') time.sleep(1) driver.quit()
其他快捷键的操作如图所示: