赞
踩
模拟键盘操作 导入包:from selenium.webdriver.common.keys import Keys 常用的键盘操作 1.send_keys(Keys.BACK_SPACE)------删除键 2.send_keys(Keys.SPACE)--------空格键 3.send_keys(Keys.TAB)------tab键 4.send_keys(Keys.ESCAPE)-------回退esc键 5.send_keys(Keys.ENTER)--------回车键 6.send_keys(Keys.CONTROL,'a')-------全选 7.send_keys(Keys.CONTROL,'c')--------复制 8.send_keys(Keys.CONTROL,'v')--------粘贴
- import time
- from selenium import webdriver
- from selenium.webdriver.common.keys import Keys
- from selenium.webdriver import ActionChains
-
- driver = webdriver.Chrome()
- driver.get("https://www.jianshu.com/sign_up")
- # 实例化鼠标对象
- action = ActionChains(driver)
- # 找到昵称输入框并输入文本admin
- nickname = driver.find_element_by_id("user_nickname")
- nickname.send_keys("admin")
- time.sleep(2)
- # 删除后两个字母
- nickname.send_keys(Keys.BACK_SPACE)
- # 输入空格键
- nickname.send_keys(Keys.SPACE)
- # 键入tab键
- nickname.send_keys(Keys.TAB)
- # 键入回退esc键
- nickname.send_keys(Keys.ESCAPE)
- # 键入回车键
- nickname.send_keys(Keys.ENTER)
- # 全选已输入的文本
- time.sleep(3)
- nickname.send_keys(Keys.CONTROL,'a')
- # 复制已输入的文本
- nickname.send_keys(Keys.CONTROL,'c')
- # 粘贴到目标输入框中
- nickname.send_keys(Keys.CONTROL,'v')
- nickname.send_keys(Keys.CONTROL,'v')
- time.sleep(3)
- driver.quit()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。