Action Chains in Selenium Python - GeeksforGeeks

19 May, 2020
Selenium’s Python Module is built to perform automated testing with Python. ActionChains are a way to automate low-level interactions such as mouse movements, mouse button actions, keypress, and context menu interactions. This is useful for doing more complex actions like hover over and drag and drop. Action chain methods are used by advanced scripts where we need to drag an element, click an element, This article revolves around how to manipulate DOM using Action Chains in Selenium. We have covered all the methods with examples int detail.
ActionChains are implemented with the help of a action chain object which stores the actions in a queue and when perform() is called, performs the queued operations.
To create object of Action Chain, import ACtion chain class from docs and pass driver as the key argument. After this one can use this object to perform all the operations of action chains.
# import webdriverfrom selenium import webdriver   # import Action chains from selenium.webdriver.common.action_chains import ActionChains   # create webdriver objectdriver = webdriver.Firefox()   # create action chain objectaction = ActionChains(driver)  
After one has created an object of Action chain, open a webpage, and perform various other methods using below syntax and examples. Action chains can be used in a chain pattern as below –
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()
Or actions can be queued up one by one, then performed.:
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()
Let’s try to implement action chains using https://www.geeksforgeeks.org/ and play around with various methods of Selenium Python.
# import webdriverfrom selenium import webdriver   # import Action chains from selenium.webdriver.common.action_chains import ActionChains   # create webdriver objectdriver = webdriver.Firefox()   # get geeksforgeeks.orgdriver.get("https://www.geeksforgeeks.org/")   # get element element = driver.find_element_by_link_text("Courses")   # create action chain objectaction = ActionChains(driver)   # click the itemaction.click(on_element = element)   # perform the operationaction.perform()
Above code, first opens https://www.geeksforgeeks.org/ and then clicks on courses button in the header, which then redirects the browser to https://practice.geeksforgeeks.org/ automatically.Output –First driver opens https://www.geeksforgeeks.org/,
then redirects to https://practice.geeksforgeeks.org/
One can perform a huge number of operations using Action chains such as click, right-click, etc. Here is a list of important methods used in Action chains.
Python-selenium
selenium
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()