0 votes
1 view
in Devops and Agile by (19.8k points)

It seems this is the way to do hover/mouseover in web driver, at least in the Java API:

Actions action = new Actions(driver);

action.moveToElement(element).build().perform();

action.moveByOffset(1, 1).build().perform();

Is this possible in the Python API? The web driver API docs for python don't seem to mention anything like it. http://selenium.googlecode.com/svn/trunk/docs/api/py/index.html

How is hover/mouseover done in python web driver?

1 Answer

0 votes
by (63.4k points)

I think you are asking for the scenarios where we need to click on the item of a drop-down list menu. We can automate it in python using Selenium.

In order to perform this action manually, first, we need to bring up the drop-down list menu by holding the mouse over the parent menu. Then click on the needed child menu from the drop-down menu displayed.

Using ActionChains class in selenium WebDriver, we can do this step in the same manner as we do manually. The method is described below -

Step 1: Import webdriver module and ActionChains class

from selenium import webdriver

from selenium.webdriver.common.action_chains

import ActionChains

Step 2: Open Firefox browser and load URL

site_url = 'Your URL'

driver = webdriver.Firefox()

driver.get(site_url)

Step 3: Create ActionChains object by passing driver object

action = ActionChains(driver);

Step 4: Notice the 1st level menu object in page and move the pointer on this object using the method ‘move_to_element()’. Method perform() is employed to execute the actions that we've got designed on action object. Do the same for all objects.

firstLevelMenu = driver.find_element_by_id("first_level_menu_id_in_your_web_page")

action.move_to_element(firstLevelMenu).perform()

secondLevelMenu = driver.find_element_by_id("second_level_menu_id_in_your_web_page")

action.move_to_element(secondLevelMenu).perform()

Step 5: Click on the desired menu item using method click()

secondLevelMenu.click()

//Final block of code is like this:

from selenium import webdriver

from selenium.webdriver.common.action_chains import ActionChains

site_url = 'Your URL'

driver = webdriver.Firefox()

driver.get(site_url)

action = ActionChains(driver);

firstLevelMenu = driver.find_element_by_id("first_level_menu_id_in_your_web_page")

action.move_to_element(firstLevelMenu).perform()

secondLevelMenu = driver.find_element_by_id("second_level_menu_id_in_your_web_page")

action.move_to_element(secondLevelMenu).perform()

secondLevelMenu.click()

You can replace driver.find_element_by_id() as per your work with the other find_elemnt methods offered in Selenium.

I hope you find this helpful!

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...