Home / Blogs / Article

Mouse Hover element using Selenide

Tutorial 17 – Mouse​​ Hover element using Selenide​​ 

What you will Learn in this blog:

  • Mouse hover (example 1)

  • Mouse hover (example 2)

  • Code snippets

Mouse hover (example 1)

Go to​​ https://www.way2automation.com/​​ 

When we mouse hover the parent menu ‘All courses’, the sub-menu is shown up

Let us see how to handle the mouse hover in selenide. Inspect ‘All Courses’​​ 

Capture ‘All Courses’ selenide element

Next, we can use the ‘actions()’ class to move to above element

The entire code looks like:

Execute.​​ 

Notice below that the sub-menu is shown

Let us now try to click the child menu element ‘Lifetime Membership’

Capture this​​ element and click it

Execute

Mouse hover (example 2)

Below is another example.​​ 

Let us try to mouse hover ‘SpiceClub’ and click child menu ‘Benefits’

 

Execute

Code snippet

package​​ w2a.selenide;

 

import​​ static​​ com.codeborne.selenide.Selenide.$;

import​​ static​​ com.codeborne.selenide.Selenide.actions;

import​​ static​​ com.codeborne.selenide.Selenide.open;

 

import​​ java.net.MalformedURLException;

 

import​​ org.openqa.selenium.By;

import​​ org.testng.annotations.Test;

 

import​​ com.codeborne.selenide.Configuration;

import​​ com.codeborne.selenide.SelenideElement;

 

public​​ class​​ MoveToElementsTest {

 

 @Test

 public​​ void​​ searchTest()​​ throws​​ InterruptedException, MalformedURLException {

  

  Configuration.browser​​ =​​ "firefox";

  /*

  open("https://www.way2automation.com/");

  

  SelenideElement allCourses = $(By.xpath("//span[text()='All Courses']"));

  

  actions().moveToElement(allCourses).build().perform();

  

  SelenideElement lifeMembership = $(By.xpath("//span[text()='Lifetime Membership']"));

  

  lifeMembership.click();

  */

  

  open("https://www.spicejet.com/");

  

  SelenideElement​​ sclub​​ =​​ $(By.xpath("//div[text()='SpiceClub']"));

  

  actions().moveToElement(sclub).build().perform();

  

  SelenideElement​​ benfts​​ =​​ $(By.xpath("//div[text()='Benefits']"));

  

  benfts.click();

  

  Thread.sleep(5000);

 }

 

}

 

Thank you for reading!

← Back to all blogs