61

读到这里,显然曾经有一个RenderedWebElement带有hover方法的类。然而,它是专门为 Java 制作的(我已经搜索了 Python 绑定文档但无济于事),并且已经被 Java 弃用。

Ahover也不能使用action_chains或使用WebElement对象来执行。

关于如何为 Python 执行此操作的任何想法?我来过这里,但它使用RenderedWebElement,因此没有太大帮助。

我正在使用:Python 2.7、Windows Vista、Selenium 2、Python 绑定

编辑:mouse_over一个selenium.selenium.selenium对象的方法,但如果没有独立服务器已经运行,我无法找到创建实例的方法。

编辑请仔细阅读标记为答案的回复评论,以防您像我一样有误解!

4

2 回答 2

120

要进行悬停,您需要使用该move_to_element方法。

这是一个例子

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()
于 2011-11-24T19:34:33.113 回答
6

@AutomatedTester 为社区提供了一个很好的解决方案!

下面是我如何使用它。

我使用信号正确退出PhantomJS,因为它有时会挂在当前进程中。

我更喜欢使用find_element_by_xpath,因为 xpath 在 chrome 中很容易找到。

就是这样:

右键单击-> 检查-> 右键单击​​-> 复制-> 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()
于 2018-07-11T05:03:36.413 回答