Selenium将鼠标光标移动到元素在Firefox中不起作用

时间:2013-07-20 14:14:23

标签: selenium selenium-webdriver automation webdriver automated-tests

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

操作系统:Win7

硒:2.33.0

Firefox:22.0

Python:2.7.4

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

我想用方法“move_to_element”将鼠标光标移动到元素“input”,但不能这样做。

有人有这个问题吗?

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

#!/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()

问题解决了。我认为move_to_element()方法应该将真正的鼠标光标移动到对象上。但是硒使鼠标悬停而不移动真正的鼠标光标。谢谢。

1 个答案:

答案 0 :(得分:6)

尝试了你的代码。你的意思不起作用?你期望发生什么?

当您将鼠标悬停在百度输入时,没有视觉效果。 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()