1

Selenium WebDriver: 2.35. FireFox: 25.0

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:

 $( 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# test code:

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();

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.

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?

1 Answer 1

5

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();

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the thought, but it didn't help. The hidden image is shown when when moving to the testDiv, but it is not clicked. It seems to be something to do with the hover event, because if I change hover to mouseenter/mouseleave then everything works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.