简体   繁体   English

使用Selenium WebDriver Java将鼠标悬停在主菜单上时不显示子菜单

[英]Not displaying submenu when mouse hover on main menu using selenium webdriver java

I am using firefox browser. 我正在使用Firefox浏览器。 I tried the mouse hover on the menu but not displaying its respective submenu when hover on the main menu using selenium webdriver.It only select the menu "Claims" and stops there only.Its not executing testScript further because on mouse hover submenu not displayed.So 我在菜单上尝试了鼠标悬停,但是在使用selenium webdriver在主菜单上悬停时未显示其相应的子菜单。它仅选择菜单“声明”并仅在此处停止。由于在鼠标悬停子菜单上未显示,因此无法进一步执行testScript。所以

HTML CODE HTML代码

    <body>
<div id="div_LockPane" class="LockOff"/>
<div id="claim-newclaimsearch" class="page-wrap" data-menu-hovered="">
<div class="header">
<div class="row">
<div class="column small-12 large-2 logo">
<nav class="column small-12 large-10 navigation-container" role="navigation">
<ul class="main-menu">
<li>
<li class="active">
<a href="/Menu/Claims">
<div class="menu-icon">
<img src="/content/common/images/menu-icons/claims.png"/>
</div>
<span class="name">Claims</span>
</a><div class="arrow-container">
<div class="sub-menu-container">
<div class="row sub-menu">
<div class="column title-side">
<h2 class="title">Claims</h2>
</div>
<div class="column points-side">
<ul>
<li class="active">
<a href="/Claim/NewClaimSearch">New Claims</a>
</li>
<div class="search-page">
<div id="search-advanced-tools" data-state="collapsed">
<div class="row">
<div class="column">
<h2 class="action-title">New Claims</h2>
</div>
</div>

I have to hover on menu "Claims" and select submenu "NEW CLAIMS" 我必须将鼠标悬停在菜单“声明”上,然后选择子菜单“新声明”

enter image description here 在此处输入图片说明

Selenium Code 硒代码

//private final By PRODUCT_CATEGORY = By.linkText("Claims");
private final By PRODUCT_CATEGORY= By.xpath("//a[contains(.,'Claims')]");
private final By PRODUCT_SUBCATEGORY = By.linkText("NEW CLAIMS");

@Test()
void testLogincase2() throws Exception{

    Thread.sleep(2000);

    WebElement hoverBtn = driver.findElement(PRODUCT_CATEGORY);
            System.out.println("click on Claims");
            Thread.sleep(2000);
            Actions action = new Actions(driver);
            Thread.sleep(2000);
            action.moveToElement(hoverBtn).perform();
            Thread.sleep(1000);

            WebElement subElement = driver.findElement(PRODUCT_SUBCATEGORY);
            Thread.sleep(1000);
            action.moveToElement(subElement);
            System.out.println("No claims");
            Thread.sleep(2000);
            action.click();
            Thread.sleep(2000);
            action.perform();

    }

As you see in below attached image here the menu "Claims" got detected by driver as it colour become a bit darker then other menu .But the submenu is not displayed like as i have shown in above image. 正如您在下面的图像中看到的那样,菜单“ Claims”已被驱动程序检测到,因为它的颜色比其他菜单要暗一些。但是,子菜单的显示不像我上图所示。 The mouse hover event is not working 鼠标悬停事件不起作用

enter image description here 在此处输入图片说明

Release ur first action before use it in second action. 释放您的第一个动作,然后再用于第二个动作。

action.moveToElement(hoverBtn).perform();

here u don't use release() method but u use this action in next.This is may be one of your problem.So for second click, try to create new action instance. 在这里,您不使用release()方法,而是在下一步中使用此操作,这可能是您的问题之一,因此对于第二次单击,请尝试创建新的操作实例。

Second, for the next click, no need to use action, i think u can do this by using normal click function i mean by: 其次,对于下一次单击,无需使用操作,我认为您可以通过使用常规单击功能来做到这一点,我的意思是:

driver.findElement(PRODUCT_SUBCATEGORY).click();

And also debug ur code properly so that ur element locator is ok or not. 还要正确调试您的代码,以便确定元素定位符是否正确。

Once you get menu on mouse hover, no need to use action again. 一旦获得鼠标悬停菜单,就无需再次使用操作。 Just click on the sub-menu. 只需单击子菜单。 Try below code after mouse hover 鼠标悬停后尝试以下代码

        WebElement subElement = driver.findElement(PRODUCT_SUBCATEGORY);
        subElement.click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM