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

浪子不回头ぞ 提交于 2019-12-22 10:49:51

问题


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

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.


回答1:


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()


来源:https://stackoverflow.com/questions/17763112/selenium-move-mouse-cursor-to-element-not-work-in-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!