Project Example –
Let’s try to implement action chains using https://www.w3wiki.org/ and play around with various methods of Selenium Python.
Python3
# import webdriver from selenium import webdriver # import Action chains from selenium.webdriver.common.action_chains import ActionChains # create webdriver object driver = webdriver.Firefox() # get w3wiki.org driver.get("https: / / www.w3wiki.org / ") # get element element = driver.find_element_by_link_text("Courses") # create action chain object action = ActionChains(driver) # click the item action.click(on_element = element) # perform the operation action.perform() |
Above code, first opens https://www.w3wiki.org/ and then clicks on courses button in the header, which then redirects the browser to https://practice.w3wiki.org/ automatically.
Output –
First driver opens https://www.w3wiki.org/,
then redirects to https://practice.w3wiki.org/
Action Chains in Selenium Python
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.