Developer Snippet Diary

ActionChains in python , Click, Click and hold, Right Click, Double Click, Drag and drop, Key Down & hold

Its do complex actions like

  • hover over
  • drag and drop
  • File Manager control
  • Control + f to open search bar etc

Syntax:

Create obj, use actions, call perform method

from selenium import webdriver # import webdriver
from selenium.webdriver.common.action_chains import ActionChains # import Action chains 
driver = webdriver.Chrome() # create webdriver object

action = ActionChains(driver) #1. create action chain object 
actions.key_down(Keys.TAB).key_up(Keys.TAB).perform() #SEND KEYS AND CALL PERFORM method

EXAMPLES:

1. click on hidden menu

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()

2.  Click

element = driver.find_element_by_link_text("PPSC MCQS")
action = ActionChains(driver)
action.click(on_element = element)
action.perform()

3. click_and_hold method is used to hold down the left mouse button on an element.

ActionChains(driver).click_and_hold(on_element = element).perform()

4. Right click

ActionChains(driver).context_click(on_element=None).perform()

5. Double Click

ActionChains(driver).double_click(on_element = element).perform()

6.Drag and Drop

source_element = driver.find_element_by_link_text("Courses")
target_element = driver.find_element_by_link_text("Hard")

ActionChains(driver).drag_and_drop(source_element, target_element).perform()

7. Kye Down and not release it.  key_up for Releases a modifier key.

ActionChains(driver).key_down(Keys.CONTROL).send_keys('F').key_up(Keys.CONTROL).perform()

8. Move Mouse current position

ActionChains(driver).move_by_offset(200, 200).perform()

9. move the mouse to the middle of an element

ActionChains(driver).move_to_element(element).perform()

10.  Pause all inputs for the specified duration in seconds

ActionChains(driver).pause(60).perform() #pause all inputs for 1 minute

11. Releasing a held mouse button on an element.

ActionChains(driver).release(on_element = element).perform()

12. Clears actions that are already stored locally and on the remote end

ActionChains(driver).reset_actions().perform()

13. send keys to current focused element.

element = driver.find_element_by_class_name("gsc-input") # get element   
action = ActionChains(driver) # create action chain object  
action.click(on_element = element) # click the item 
action.send_keys("Hello") # send keys  
action.perform() # perform the operation

OR
actions = ActionChains(bot)
actions.key_down(Keys.TAB).key_up(Keys.TAB).perform()

14. Scroll to element 

from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.move_to_element(element).perform()

 

perform method is used to perform all stored operations in action instance of ActionChains class.

 

 

Posted by: R GONDAL
Email: rizikmw@gmail.com