Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mouseover in python Webdriver

It seems this is the way to do hover / mouseover in webdriver, 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 webdriver 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 webdriver?

like image 791
Purrell Avatar asked Jun 19 '12 00:06

Purrell


2 Answers

from selenium.webdriver.common.action_chains import ActionChains   def hover(self):     wd = webdriver_connection.connection     element = wd.find_element_by_link_text(self.locator)     hov = ActionChains(wd).move_to_element(element)     hov.perform() 
like image 161
user1411110 Avatar answered Oct 13 '22 16:10

user1411110


I think you are asking for 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 mouse over the parent menu. Then click on required child menu from drop down menu displayed.

Using ActionChains class in Selenium WebDriver, we can do this step in 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: Find first level menu object in page and move cursor on this object using method ‘move_to_element()’. Method perform() is used to execute the actions that we have built 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 required 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() according to your work with any other find_elemnt method available in selenium. Hope It will be helpful for you.

like image 42
Tanmoy Datta Avatar answered Oct 13 '22 14:10

Tanmoy Datta