简体   繁体   English

Selenium Web驱动程序/ Java /悬停+单击

[英]Selenium web driver / java / hover + click

I am looking to click a menu link, but the link only appears if the cursor hovers above it stretching the drop down. 我希望单击菜单链接,但是仅当光标悬停在其上方并拉长下拉菜单时,该链接才会出现。 Therefore, the automation is unable to click it like it would normally with my click function. 因此,自动化无法像我的单击功能一样单击它。 I did some research and used moveToElement , and clickAndHold . 我做了一些研究,并使用moveToElementclickAndHold The latter has given me some hope, but it is far from perfect. 后者给了我一些希望,但远非完美。 I am finding it not clicking at all half the time, and sometimes it does click but clicks a different menu link in the drop down. 我发现它没有完全单击一半,有时它确实单击了,但是单击了下拉菜单中的其他菜单链接。 Any ideas how I can make it work 100% of the time? 有什么想法可以使它100%地起作用吗?

public  String hoverClick(String object, String data){
    APP_LOGS.debug("Moving the mouse");
    try{
        WebElement tab;
        WebElement link;
        tab = driver.findElement(By.xpath("//a[contains(@href, 'FOO')]"));                                  
        link = driver.findElement(By.xpath("//a[contains(@href, 'BAR')]"));

        Actions act = new Actions(driver);
        act.clickAndHold(tab).click(link).perform();
        return Constants.KEYWORD_PASS;
    }catch(Exception e){
        return Constants.KEYWORD_FAIL+"Unable to move the mouse/click"+e.getMessage();
    }
}    

Thank you. 谢谢。

Please give a try with following: 请尝试以下方法:

act.moveToElement(tab).moveToElement(link).click(link).perform();

Here, we hover over the element which populates dropdown(and which contains element to be clicked on) and then move to element to be clicked on and then click it. 在这里,我们将鼠标悬停在填充下拉列表的元素上(该元素包含要单击的元素),然后移至要单击的元素,然后单击它。

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

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