简体   繁体   中英

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 . 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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