1.常用的键盘操作:
send_keys(Keys.BACK_SPACE) 删除键(BackSpace)
send_keys(Keys.SPACE) 空格键(Space)
send_keys(Keys.TAB) 制表键(Tab)
send_Keys(Keys.CONTROL,'a') 全选(Ctrl+A)
from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Firefox() driver.get("http://www.baidu.com") driver.find_element_by_id("kw").sends_keys("selenium") driver.find_element_by_id("kw").send_keys(Keys.BACK_SPACE) driver.find_element_by_id("kw").send_keys(Keys.SPACE)#输入空格键
2.获得验证信息
from selenium import webdriver import time driver = webdriver.Firefox() driver.get("http://www.126.com") print('Before login==========') title = driver.title print(title) now_url = driver.current_url print(now_url) #执行邮箱登录 driver.find_element_by_id("idInput").clear() driver.find_element_by_id("idInput").send_keys("username") driver.find_element_by_id("pwdInput").clear() driver.find_element_by_id("pwdInput").send_keys("password") driver.find_element_by_id("loginBtn").click() time.sleep(5) print('After login===========') #再次打印当前页面title title = driver.title print(title) #打印当前页URL now_url = driver.current_url print(now_url) #获得登录的用户名 user = driver.find_element_by_id('spnUid').text print(user) driver.quit()
title:用户获得当前页面的标题
current_url:用户获得当前页面的URL
3.设置元素等待:
WebDriver提供了两种等待:显示等待和隐式等待