In Previous section, we have discussed about the handling the Dropdown which does not have Select – Option tag such as JQuery Dropdown. Now we will concentrate about the Mouse movement like Drag & Drop, Move to Element, Right Click, User Actions etc. in this section. We can have a look on what all different action are available in Action Class.
- Test Scenario
In Selenium Python there is a concept called ActionChains class which will help to work with Selenium Python for Move to Element, Double click, Right Click, Drag & Drop. There are some important methods are there which are very simple and we will discuss on that in this section.
In this tutorial we will automate the SpiceJet website for learning purpose to explore Move to Element with Selenium Python. In this you can see some Menu item showing on top. While you hover on that you can see the sub menu listed. Observe the scenario here that in this case you do not need to click on the menu item. You just need to mouse hover on the Menu item and see the sub menu list. To achieve this finding the web element and clicking on item will not work. For this we need to move our mouse pointer to the corresponding menu item.
Have a look on above image of SpiceJet Home page. Here if you want to click on Student Discount sub menu option which is inside ADD-ONS menu, then initially searching this item will throw and ElementNotFoundException as it sub menu will not be opened while you open the Home page. For this you need to hover on the Menu Item.
To automate this scenario we need a concept called ActionChains class in Selenium Python.
from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(ChromeDriverManager().install()) driver.implicitly_wait(10) driver.get('https://www.spicejet.com/') driver.maximize_window() ''' Move to Element ''' add_on = driver.find_element(By.ID, 'highlight-addons') act_chain = ActionChains(driver) act_chain.move_to_element(add_on).perform()
From the above example you can notice that we have imported ActionChains class which helps us to move the mouse from one element to another element. To work with ActionChains we first we need to find the web element to which we want to move our mouse. Then we will create the the ActionChains object by providing the driver object as argument to the ActionChains(). After we created the ActionChains object we can call the move_to_element() and pass element to which we want our mouse pointer should move. After all these, we have to call perform() function at last which is mandatory. These 3 steps will execute our Mouse movement on Selenium Python.
add_on = driver.find_element(By.ID, 'highlight-addons') act_chain = ActionChains(driver) act_chain.move_to_element(add_on).perform()
- In Selenium Python there is a concept called ActionChains class which will help to work with Selenium Python for Move to Element, Double click, Right Click, Drag & Drop.
Now you are clear about the move to element with Selenium Python and hands-on with ActionChains class. Next section we will discuss about some more advance topic mouse movement like Drag and Drop.