66

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 !

Ashwin
  • 1,190
  • 2
  • 10
  • 30

2 Answers2

129

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()
AutomatedTester
  • 22,188
  • 7
  • 49
  • 62
  • the last line should be "hover.perform()" Yes I have tried that out but when I said hover I had this idea that when I hovered over an element it would show me the text which is supposed to be displayed (like if u hover over the "flag" element,just above this comment you get a "Flag this post for serious problems or moderator attention" popout (I dont know what to call it)) Any ideas? – Ashwin Nov 25 '11 at 04:20
  • that has nothing to do with hovering, the CSS being applied does but what you are after is the title of the element. For that you need to use `get_attribute('title')` on the WebElement – AutomatedTester Nov 25 '11 at 09:05
  • I thought that was the way to get it....was wrong I guess....thanks for giving the right way to go about it! – Ashwin Nov 26 '11 at 19:15
  • Any idea when this will be supported on mac? I get the "Cannot perform native interaction" error – Bob Evans Nov 28 '11 at 16:17
  • 2
    to add to my comment from almost a year ago, this appears to be addressed on Mac. I'm using 2.26 python bindings and no longer get "cannot perform native interaction" error – Bob Evans Nov 06 '12 at 19:40
7

@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()
frianH
  • 7,295
  • 6
  • 20
  • 45
hyukkyulee
  • 1,024
  • 1
  • 12
  • 17