from selenium import webdriver from selenium.webdriver.comm

时间:2021-12-21 02:12:02

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
# 导入供给鼠标操纵的ActionChains
driver = webdriver.Chrome()
driver.get(‘‘)
driver.maximize_window() #最大化当前窗口
driver.execute_script(‘window.scrollTo(0,0);‘)
#执行JavaScript语句,,通过js 来操纵滚动条
right_click = driver.find_element_by_id(‘id1‘)
ActionChains(driver).context_click(right_click).perform()

ActionChains(driver)
挪用ActionChains() 类,将浏览器驱动driver作为参数传入
context_click(right_click) context_click( )要领用于模拟鼠标右键操纵,在挪用时需要指定元素定位 perform()
执行所有ActionChains 中存储的行为,可以理解成是对整个操纵的提交行动
3.2 鼠标悬停 move_to_element()
move_to_element() 要领可以模拟鼠标悬停的行动,其用法与context_click( )不异 dive = driver.find_element_by_css_selector(‘#a‘)
ActionChains(driver).move_to_element(dive).perform()
3.3 鼠标双击操纵 double_click()

3.4 鼠标拖放操纵 drag_and_drop(source,target)
source 鼠标拖动的源元素
target 鼠标释放的方针元素
s = driver.find_element_by_css_selector(‘#dragger‘)
t = driver.find_element_by_css_selector(‘#i1‘)
ActionChains(driver).drag_and_drop(s,t).perform()   引用要领 :from selenium.webdriver.common.keys import Keys # key_down 模拟键盘摁下某个按键 key_up 松开某个按键,与sendkey连用完成一些操纵,每次down必需up一次否则将呈现异常
ActionChains(driver).key_down(Keys.CONTROL,dom).send_keys(‘a‘).send_keys(‘c‘).
key_up(Keys.CONTROL).key_down(Keys.CONTROL,dom1).send_keys(‘v‘).key_up(Keys.CONTROL).perform()