In this tutorial, we are going to learn about mouse hover assigned an element using selenium python. To get better understanding of the topic first let us understand what is selenium python and what is mouse hover action clearly in this topic.
What is selenium python.?
Selenium is one of the most popular automation tools and it is used to testing tool. It is basically free open-source testing tool. It deals with the web, it also possesses various web elements. This web elements are nothing but a representation of HTML elements.
What is mouse hover action..?
In selenium python, a mouse hover action is used to test an elements behaviour when it has hovered over.It basically involves using Actions class to create a new Action object and then using moveToElement method to move the mouse cursor over the element.
HERE ARE THE STEPS…
To perform any mouse hover assigned an element using Selenium python, firstly you must use Actions class from the selenium.webdriver.common.action_chains modules.
THERE ARE FIVE STEPS INVOLVED:
- Import the necessary modules.
from selenium import webdriver from seleinum.webdriver.common.action_chains import Action Chains
- Now create new instance of the webdriver (eg: chrome,firefox,..etc) and navigate to the webpage.
driver = webdriver.firefox() driver.get("Paste link")
- Locate the elements you want to hover over using a reliable locator.
driver.find_element_by_xpath("address")
- Again create an instance of the Action Chain class and insert the webdriver.
actions = Action Chains(driver)
- Now using the move_to_element() method of the Action Chains class to perform the mouse hover action.
actions.move_to_element(element).perform()
Here is the complete code:
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains driver = webdriver.firefox() element = driver.find_element_by_xpath("address") actions = ActionChains(driver) actions.move_to-element(element).perform() driver.quit()