Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to perform a mouseover (hover over an element) using Selenium and Python bindings?

Reading here, there apparently used to be a RenderedWebElement class with a hover method. It, however, was exclusively made for Java (I have searched the Python bindings documentation to no avail) and has since been deprecated for Java.

A hover can't be performed using action_chains nor by using a WebElement object either.

Any ideas as to how to do this for Python? I have been here but it uses RenderedWebElement and hence doesn't help too much.

I am using: Python 2.7, Windows Vista, Selenium 2, Python Bindings

EDIT: There is a method mouse_over for a selenium.selenium.selenium object but I cannot figure a way to create an instance without having the stand-alone server running already.

EDIT Please go through the comments of the reply marked as answer just in-case you have misconceptions like I did !

like image 617
Ashwin Avatar asked Nov 24 '11 05:11

Ashwin


People also ask

How do you mouse hover in Selenium with python?

We can perform mouseover action in Selenium webdriver in Python by using the ActionChains class. We have to create an object of this class and then apply suitable methods on it. In order to move the mouse to an element, we shall use the move_to_element method and pass the element locator as a parameter.

How do I make my mouse hover in Selenium?

The first step here would be to locate the main menu (AKA parent menu). Once that is done, the second step is to locate the desired element (child element) from the available options in the sub-menu. The final step would be to click on that child element.

Which method of actions class is used to mouse hover an element in Selenium?

We can perform mouseover action on elements in Selenium with the help of Actions class. In order to perform the mouse movement we will use moveToElement () method of the Actions class.

Which command will help you to mouse hover on a control in Selenium?

So, here are the methods Actions class has provided for Mouse Hover action: moveToElement(WebElement target) moveToElement(WebElement target, int xOffset, int yOffset)


2 Answers

To do a hover you need to use the move_to_element method.

Here is an example

from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains  firefox = webdriver.Firefox() firefox.get('http://foo.bar') element_to_hover_over = firefox.find_element_by_id("baz")  hover = ActionChains(firefox).move_to_element(element_to_hover_over) hover.perform() 
like image 173
AutomatedTester Avatar answered Sep 20 '22 01:09

AutomatedTester


@AutomatedTester have given the community a great solution!

Below is how I used it.

I used signal to properly quit PhantomJS since it sometimes hangs in the current process.

I prefer to use find_element_by_xpath since xpath can be easily found in chrome.

Here's how:

Right click -> Inspect -> Right click -> Copy -> CopyXpath

from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import signal  browser = webdriver.PhantomJS() browser.implicitly_wait(3)  def hover(browser, xpath):     element_to_hover_over = browser.find_element_by_xpath(xpath)     hover = ActionChains(browser).move_to_element(element_to_hover_over)     hover.perform()    browser.service.process.send_signal(signal.SIGTERM)  # kill the specific phantomjs child proc browser.quit() 
like image 21
hyukkyulee Avatar answered Sep 20 '22 01:09

hyukkyulee