Selenium WebDriver MoveToElement-隐藏的元素,悬停和toggleClass [英] Selenium WebDriver MoveToElement - hidden element, hover and toggleClass

查看:607
本文介绍了Selenium WebDriver MoveToElement-隐藏的元素,悬停和toggleClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Selenium WebDriver:2.35. FireFox:25.0

Selenium WebDriver: 2.35. FireFox: 25.0

我想将鼠标移到div上,这将导致隐藏的图像变得可见,然后单击该图像.我已经在此处阅读了帖子,此处及其他.一般的答案是做某种形式的:

I want to move the mouse over a div which will causes a hidden image to become visible, then click the image. I have read the posts here, here, here and others. The general answer is to do something of the form:

action.moveToElement(we).moveToElement(webdriver.findElement(By.xpath("/expression-here")).click().build().perform();

但是,在下面的示例中这是行不通的:

However, this doesn't work in the example below:

html:

<div id="bb_testDiv">
    <img class="bb_matchImgTest bb_standardHidden" src='@Url.Content( "~/images/match.png" )' alt='Match'/>
</div>

Javascript/jquery:

Javascript/jquery:

 $( document ).on( 'hover', '#bb_testDiv', function ()
 {           
     $( this ).find( '.bb_matchImgTest' ).toggleClass( 'bb_standardHidden' );
 } )        
 $( document ).on( 'click', '.bb_matchImgTest', function ()
 {
     alert('here');
 } )

CSS:

.bb_standardHidden
{
    visibility:hidden;
}

C#测试代码:

IWebElement testDiv = WebDriver.FindElement( By.Id( "bb_testDiv" ) );            
Actions builder = new Actions( WebDriver );
Actions hoverClick = builder.MoveToElement( testDiv ).MoveToElement( testDiv.FindElement( By.ClassName( "bb_matchImgTest" ) ) ).Click();
hoverClick.Build().Perform();

问题在于click事件未触发.同样,该元素保持可见状态,因此任何后续的鼠标悬停都将其隐藏.当然,通过手动测试,这一切都可以正常工作.

The problem is that the click event is not fired. Also, the element is left visible, so any subsequent mouseover hides it. Of course, this all works fine with manual testing.

问题似乎出在悬停事件上.如果我将其分为两个事件-使用addClass和removeClass(而不是toggleClass)的mouseenter和mouseleave(而不是悬停),那么它将起作用.只是想知道是否有可能将其与悬停一起使用?

The problem seems to be with the hover event. If I break this into two events - mouseenter and mouseleave (instead of hover) with addClass and removeClass (instead of toggleClass) then it works. Just wondering if it is possible to get this working with hover?

推荐答案

由于隐藏了元素.bb_standardHidden,这可能是原因,因为MoveToElement无法按照您的需要工作.

As the element .bb_standardHidden is hidden this could be the cause because the MoveToElement is not working as you need.

IWebElement testDiv = WebDriver.FindElement( By.Id( "bb_testDiv" ) );            
Actions builder = new Actions( WebDriver );
Actions hoverClick = builder.MoveToElement( testDiv ).MoveByOffset( x, y ).Click();
hoverClick.Build().Perform();

使用x和y来确保鼠标将位于隐藏的图片上.

with x and y to make sure the mouse will be over the hidden picture.

这篇关于Selenium WebDriver MoveToElement-隐藏的元素,悬停和toggleClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆