3

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

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.

4

1 回答 1

7

试过你的代码。你的意思是行不通?你期望会发生什么?

将鼠标悬停在百度输入上时没有视觉效果。Selenium 在不移动真实鼠标的情况下移动到元素,因此您不会看到真实鼠标光标的位置变化。

如果你真的想测试move_to_element,请针对具有悬停效果的东西进行测试,这样你就可以直观地看到它。

这是一个例子:

#!/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()
于 2013-07-20T21:41:12.527 回答