Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium move mouse cursor to element not work in Firefox [closed]

================================

OS: Win7

Selenium: 2.33.0

Firefox: 22.0

Python: 2.7.4

================================

I want to move the mouse cursor to the element "input" with method "move_to_element", but can't do it.

Do anyone have this issue?

================================

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from selenium.webdriver.common.by import By

import selenium.webdriver as webdriver
import time


firefox = webdriver.Firefox()

firefox.get("http://www.baidu.com")


input = firefox.find_element_by_id("kw")

action = webdriver.ActionChains(firefox)
action.send_keys_to_element(input, "testvalue")
action.perform()

#This step (move mouse to "input" element) NOT work! :(
action = webdriver.ActionChains(firefox)
action.move_to_element(input)
action.perform()


time.sleep(3)
firefox.quit()

Problem solved. I thought move_to_element() method should move the real mouse cursor to the object. But selenium does the mouse hover without moving real mouse cursor. Thanks.

like image 495
HWW Avatar asked Dec 05 '25 19:12

HWW


1 Answers

Tried your code. What you do mean doesn't work? What do you expect to happen?

There is no visual effect when you hover to Baidu's input. Selenium moves to the element without moving the real mouse, so you won't see the position change of the real mouse cursor.

If you really want to test move_to_element, please test against something that has hover effect, so you can visually see it.

Here's an example:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from selenium.webdriver.common.by import By

import selenium.webdriver as webdriver
import time


firefox = webdriver.Firefox()

firefox.get("http://stackoverflow.com/tags")


tags = firefox.find_elements_by_css_selector("#tags-browser .tag-cell .post-tag")

action = webdriver.ActionChains(firefox)
action.move_to_element(tags[0])
action.perform()
like image 126
Yi Zeng Avatar answered Dec 09 '25 17:12

Yi Zeng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!