ActionChains用法(move_to_element()/drag_and_drop())

锺离良哲
2023-12-01

Step1.导入ActionChains

from selenium.webdriver.common.action_chains import ActionChains

1.定义鼠标悬停的元素:

move = driver.find_element_by_css_selector(
                'div.queryschema-control.ivu-col.ivu-col-span-16 > div > div > div > input')
2.模拟鼠标悬停:

ActionChains(driver).move_to_element(move).perform()
3.操作悬停后出现的元素

 

menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")
 
actions = ActionChains(driver)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()

 

 

ActionChains方法列表

click(on_element=None) ——单击鼠标左键

click_and_hold(on_element=None) ——点击鼠标左键,不松开

context_click(on_element=None) ——点击鼠标右键

double_click(on_element=None) ——双击鼠标左键

drag_and_drop(source, target) ——拖拽到某个元素然后松开

drag_and_drop_by_offset(source, xoffset, yoffset) ——拖拽到某个坐标然后松开

key_down(value, element=None) ——按下某个键盘上的键

key_up(value, element=None) ——松开某个键

move_by_offset(xoffset, yoffset) ——鼠标从当前位置移动到某个坐标

move_to_element(to_element) ——鼠标移动到某个元素

move_to_element_with_offset(to_element, xoffset, yoffset) ——移动到距某个元素(左上角坐标)多少距离的位置

perform() ——执行链中的所有动作

release(on_element=None) ——在某个元素位置松开鼠标左键

send_keys(*keys_to_send) ——发送某个键到当前焦点的元素

send_keys_to_element(element, *keys_to_send) ——发送某个键到指定元素

 

 

 

点击操作

示例网址http://sahitest.com/demo/clicks.htm

代码:

 
  1. # -*- coding: utf-8 -*-

  2.  
  3. from selenium import webdriver

  4. from selenium.webdriver.common.action_chains import ActionChains

  5. from time import sleep

  6.  
  7.  
  8. driver = webdriver.Firefox()

  9. driver.implicitly_wait(10)

  10. driver.maximize_window()

  11. driver.get(‘http://sahitest.com/demo/clicks.htm‘)

  12.  
  13. click_btn = driver.find_element_by_xpath(‘//input[@value="click me"]‘) # 单击按钮

  14. doubleclick_btn = driver.find_element_by_xpath(‘//input[@value="dbl click me"]‘) # 双击按钮

  15. rightclick_btn = driver.find_element_by_xpath(‘//input[@value="right click me"]‘) # 右键单击按钮

  16.  
  17.  
  18. ActionChains(driver).click(click_btn).double_click(doubleclick_btn).context_click(rightclick_btn).perform() # 链式用法

  19.  
  20. print driver.find_element_by_name(‘t2‘).get_attribute(‘value‘)

  21.  
  22. sleep(2)

  23. driver.quit()

结果:

[CLICK][DOUBLE_CLICK][RIGHT_CLICK]

2.鼠标移动

示例网址http://sahitest.com/demo/mouseover.htm

示例代码:

 
  1. # -*- coding: utf-8 -*-

  2.  
  3. from selenium import webdriver

  4. from selenium.webdriver.common.action_chains import ActionChains

  5. from time import sleep

  6.  
  7. driver = webdriver.Firefox()

  8. driver.implicitly_wait(10)

  9. driver.maximize_window()

  10. driver.get(‘http://sahitest.com/demo/mouseover.htm‘)

  11.  
  12. write = driver.find_element_by_xpath(‘//input[@value="Write on hover"]‘) # 鼠标移动到此元素,在下面的input框中会显示“Mouse moved”

  13. blank = driver.find_element_by_xpath(‘//input[@value="Blank on hover"]‘) # 鼠标移动到此元素,会清空下面input框中的内容

  14.  
  15. result = driver.find_element_by_name(‘t1‘)

  16.  
  17. action = ActionChains(driver)

  18. action.move_to_element(write).perform() # 移动到write,显示“Mouse moved”

  19. print result.get_attribute(‘value‘)

  20.  
  21. # action.move_to_element(blank).perform()

  22. action.move_by_offset(10, 50).perform() # 移动到距离当前位置(10,50)的点,与上句效果相同,移动到blank上,清空

  23. print result.get_attribute(‘value‘)

  24.  
  25. action.move_to_element_with_offset(blank, 10, -40).perform() # 移动到距离blank元素(10,-40)的点,可移动到write上

  26. print result.get_attribute(‘value‘)

  27.  
  28. sleep(2)

  29. driver.quit()

结果

 
  1. Mouse moved

  2.  
  3. Mouse moved

一般很少用位置关系来移动鼠标,如果需要,可参考下面的链接来测量元素位置

http://jingyan.baidu.com/article/eb9f7b6d87e2ae869264e847.html

3.拖拽

示例网址http://sahitest.com/demo/dragDropMooTools.htm

代码:

 
  1. # -*- coding: utf-8 -*-

  2.  
  3. from selenium import webdriver

  4. from selenium.webdriver.common.action_chains import ActionChains

  5. from time import sleep

  6.  
  7. driver = webdriver.Firefox()

  8. driver.implicitly_wait(10)

  9. driver.maximize_window()

  10. driver.get(‘http://sahitest.com/demo/dragDropMooTools.htm‘)

  11.  
  12. dragger = driver.find_element_by_id(‘dragger‘) # 被拖拽元素

  13. item1 = driver.find_element_by_xpath(‘//div[text()="Item 1"]‘) # 目标元素1

  14. item2 = driver.find_element_by_xpath(‘//div[text()="Item 2"]‘) # 目标2

  15. item3 = driver.find_element_by_xpath(‘//div[text()="Item 3"]‘) # 目标3

  16. item4 = driver.find_element_by_xpath(‘//div[text()="Item 4"]‘) # 目标4

  17.  
  18. action = ActionChains(driver)

  19. action.drag_and_drop(dragger, item1).perform() # 1.移动dragger到目标1

  20. sleep(2)

  21. action.click_and_hold(dragger).release(item2).perform() # 2.效果与上句相同,也能起到移动效果

  22. sleep(2)

  23. action.click_and_hold(dragger).move_to_element(item3).release().perform() # 3.效果与上两句相同,也能起到移动的效果

  24. sleep(2)

  25. # action.drag_and_drop_by_offset(dragger, 400, 150).perform() # 4.移动到指定坐标

  26. action.click_and_hold(dragger).move_by_offset(400, 150).release().perform() # 5.与上一句相同,移动到指定坐标

  27. sleep(2)

  28. driver.quit()

结果:

dropped dropped dropped dropped

一般用坐标定位很少,用上例中的方法1足够了,如果看源码,会发现方法2其实就是方法1中的drag_and_drop()的实现。注意:拖拽使用时注意加等待时间,有时会因为速度太快而失败。

4.按键

模拟按键有多种方法,能用win32api来实现,能用SendKeys来实现,也可以用selenium的WebElement对象的send_keys()方法来实现,这里ActionChains类也提供了几个模拟按键的方法。

示例网址http://sahitest.com/demo/keypress.htm

代码1:

 
  1. # -*- coding: utf-8 -*-

  2.  
  3. from selenium import webdriver

  4. from selenium.webdriver.common.action_chains import ActionChains

  5. from time import sleep

  6.  
  7. driver = webdriver.Firefox()

  8. driver.implicitly_wait(10)

  9. driver.maximize_window()

  10. driver.get(‘http://sahitest.com/demo/keypress.htm‘)

  11.  
  12. key_up_radio = driver.find_element_by_id(‘r1‘) # 监测按键升起

  13. key_down_radio = driver.find_element_by_id(‘r2‘) # 监测按键按下

  14. key_press_radio = driver.find_element_by_id(‘r3‘) # 监测按键按下升起

  15.  
  16. enter = driver.find_elements_by_xpath(‘//form[@name="f1"]/input‘)[1] # 输入框

  17. result = driver.find_elements_by_xpath(‘//form[@name="f1"]/input‘)[0] # 监测结果

  18.  
  19. # 监测key_down

  20. key_down_radio.click()

  21. ActionChains(driver).key_down(Keys.CONTROL, enter).key_up(Keys.CONTROL).perform()

  22. print result.get_attribute(‘value‘)

  23.  
  24. # 监测key_up

  25. key_up_radio.click()

  26. enter.click()

  27. ActionChains(driver).key_down(Keys.SHIFT).key_up(Keys.SHIFT).perform()

  28. print result.get_attribute(‘value‘)

  29.  
  30. # 监测key_press

  31. key_press_radio.click()

  32. enter.click()

  33. ActionChains(driver).send_keys(‘a‘).perform()

  34. print result.get_attribute(‘value‘)

  35. driver.quit()

结果:

 
  1. key downed charCode=[0] keyCode=[17] CTRL

  2. key upped charCode=[0] keyCode=[16] NONE

  3. key pressed charCode=[97] keyCode=[0] NONE

示例2:

示例网址http://sahitest.com/demo/label.htm

代码:

 
  1. # -*- coding: utf-8 -*-

  2.  
  3. from selenium import webdriver

  4. from selenium.webdriver.common.action_chains import ActionChains

  5. from selenium.webdriver.common.keys import Keys

  6. from time import sleep

  7.  
  8. driver = webdriver.Firefox()

  9. driver.implicitly_wait(10)

  10. driver.maximize_window()

  11.  
  12. driver.get(‘http://sahitest.com/demo/label.htm‘)

  13.  
  14. input1 = driver.find_elements_by_tag_name(‘input‘)[3]

  15. input2 = driver.find_elements_by_tag_name(‘input‘)[4]

  16.  
  17. action = ActionChains(driver)

  18. input1.click()

  19. action.send_keys(‘Test Keys‘).perform()

  20. action.key_down(Keys.CONTROL).send_keys(‘a‘).key_up(Keys.CONTROL).perform() # ctrl+a

  21. action.key_down(Keys.CONTROL).send_keys(‘c‘).key_up(Keys.CONTROL).perform() # ctrl+c

  22.  
  23. action.key_down(Keys.CONTROL, input2).send_keys(‘v‘).key_up(Keys.CONTROL).perform() # ctrl+v

  24.  
  25. print input1.get_attribute(‘value‘)

  26. print input2.get_attribute(‘value‘)

  27.  
  28. driver.quit()

结果:

 
  1. Test Keys

  2. Test Keys

复制粘贴用WebElement< input >.send_keys()也能实现,大家可以试一下,也可以用更底层的方法,同时也是os弹框的处理办法之一的win32api,有兴趣也可以试试SendKeys、keybd_event

 类似资料: