基于www.baidu.com网站测试,使用火狐浏览器,代码如下:
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
#打开浏览器
dr = webdriver.Firefox()
dr.get("https://www.baidu.com")
#点击登录按钮
dr.find_element_by_link_text("登录").click()
sleep(3)
#点击 用户登录
dr.find_element_by_id("TANGRAM__PSP_10__footerULoginBtn").click()
#输入用户名
# element = dr.find_element_by_id("passport-login-pop-api")
userName = dr.find_element_by_id("TANGRAM__PSP_10__userName")
userName.send_keys("username")
sleep(2)
#模拟Backspace退格键删除
userName.send_keys(Keys.BACKSPACE)
sleep(1)
#模拟全选键ctrl + a
userName.send_keys(Keys.CONTROL,'a')
sleep(1)
#模拟键盘的剪切ctrl + x
userName.send_keys(Keys.CONTROL,'x')
sleep(2)
#模拟键盘的粘贴ctrl + v
userName.send_keys(Keys.CONTROL,'v')
sleep(3)
dr.quit()