Selenium 和 :hover css

2022-01-22 00:00:00 selenium testing hover selenium-rc css

使用 selenium-rc 和 java 客户端,我想测试一个菜单,当用户将鼠标移到它上面时会打开该菜单.当鼠标离开菜单时它会关闭.这是使用 :hover css 完成的,没有任何 javascript.

Using selenium-rc and the java client, I want to test a menu which opens when the user moves the mouse over it. It closes when the mouse leaves the menu. This is done using :hover css, without any javascript.

在 selenium 中,有很多鼠标操作的方法,但似乎没有一个会触发任何要使用的 css :hover 样式.

In selenium, there are many methods for mouse actions, but none of them seems to trigger any css :hover style to be used.

Google 显示我并不孤单,但还没有解决方案.有些人评论说您必须添加一些 javascript 代码;但是,在 selenium rc 中,我认为我什至没有合适的位置来存放用户贡献的附加 javascript 代码.

Google shows that I am not alone with this problem, but there has not be a solution. Some folks comment that you had to add some javascript code; however, in selenium rc, I don't think that I even have a proper place for user-contributed additional javascript code.

我希望下面的代码能够工作,因为 div#navi_settings 包含的菜单包含 - 通常不可见 - 一个元素:

My wish would be the following code to work, given that a div#navi_settings contained the menu which contains the - normally invisible - a element:

selenium.mouseHover("css=div#navi_settings");
assertTrue(selenium.isVisible("//a[contains(text(), 'Text on link')]"));

很遗憾,moveHover() 方法尚不存在.

Unfortunately, the method moveHover() does not yet exist.

推荐答案

我找不到使用 Selenium 接口的方法.但是,由于我使用的是 Selenium 2,我可以使用 WebDriver API,根据 http://groups.google.com/group/selenium-developers/msg/8210537dde07155f?pli=1

I couldn't find a way to do this using the Selenium interface. However, since I am using Selenium 2, I can use the WebDriver API, as per http://groups.google.com/group/selenium-developers/msg/8210537dde07155f?pli=1

在你的情况下,如果你可以升级到 Selenium 2,这样的事情可能会起作用:

In your case, something like this may work, if you can upgrade to Selenium 2:

WebDriver webDriver; 
...
((RenderedWebElement) webDriver.findElement(By.cssSelector("div#navi_settings"))).hover();

相关文章