Mouse hover Element in Playwright Java

Profile picture for user arilio666

In this article, we will see how we can hover over elements in playwright. This is achieved using the hover() method available in the playwright.

  • This menu here is opened when hovered over it. Let us try to click quality assurance from the tutorial and get the page's title.
try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
            Page page = browser.newPage();
            page.navigate("http://www.programsbuzz.com/");
            Locator tutorial = page.locator("//a[@class='we-mega-menu-li'][normalize-space()='Tutorials']");
            tutorial.hover();
            page.locator("//a[@class='we-mega-menu-li'][normalize-space()='Quality Assurance']").click();
            System.out.println(page.title());
            browser.close();
            playwright.close();
        }
        
  • As we can see, the menu has hovered, and the quality assurance has been selected.
  • We can confirm this from the title we just printed.