鼠标及键盘操作
鼠标操作:1、perform():执行 2、context_click():右击 3、double_click():双击 4、drag_and_drop():拖动 5、move_to_element():鼠标悬停
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import time driver = webdriver.Firefox() driver.get("https://www.baidu.com") time.sleep(2) #定位到要悬停的元素 mouse = driver.find_element_by_link_text("设置") #悬停操作 ActionChains(driver).move_to_element(mouse).perform()
键盘操作:
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time driver = webdriver.Firefox() driver.get("https://www.baidu.com") time.sleep(2) driver.find_element_by_id("kw").send_keys("selenium") time.sleep(2) #backspace键,英文大写 driver.find_element_by_id("kw").send_keys(Keys.BACKSPACE) time.sleep(2) #清除输入框内容 driver.find_element_by_id("kw").clear() time.sleep(2) driver.find_element_by_id("kw").send_keys("python") time.sleep(2) #Ctrl+a driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a') time.sleep(2) driver.quit()