1、基础操作
2、ActionChains 类
ActionChains顾名思义就是一个动作链,将一些操作存储在一个队列中,当执行perform()时,队列中的操作会按照顺序执行,执行后队列被清空。
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
browser =webdriver.Chrome()
browser.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
browser.switch_to.frame('iframeResult') # id = 'iframeResult'
source = browser.find_element_by_css_selector('#draggable') # 被拖拽的对象
target = browser.find_element_by_css_selector('#droppable') # 目标对象
actions = ActionChains(browser)
actions.drag_and_drop(source, target)
actions.perform()
time.sleep(3)
browser.close()
也可以写一长串
menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")
ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
time.sleep(3)
# 实现Ctrl+F5刷新
ActionChains(driver).key_down(Keys.ALT).send_keys(Keys.F5).key_up(Keys.CONTROL).perform()