简体   繁体   中英

Unable to click a mouse hover link using Selenium WebDriver with Java

I am trying to click a mouse hover link using the code below. The webdriver (v.2.35) doesn't throw any error but the element isn't clicked. Can somebody help me figure out what's wrong?

String URL = "http://www.kgisliim.ac.in/"
String menu ="Alumni>Register"

driver.get(URL);
String[] menuItems = menu.split(">");
Actions actions = new Actions(driver);
WebElement tempElem;
for (int i =0 ; i< menuItems.length ; i++) {                     
   tempElem =  driver.findElement(By.linkText(menuItems[i].trim()));
   actions.moveToElement(tempElem).build().perform();
}
actions.click();
actions.perform();

NOTE: The above code works fine in the below scenario

String URL = "http://www.flipkart.com/"
String menu ="Clothing>Jeans"

You can try this:

WebDriver driver=new FirefoxDriver();
        driver.get("http://www.kgisliim.ac.in/");
        Actions  actions=new Actions(driver);
        WebElement menuHoverLink=driver.findElement(By.linkText("Alumni"));
        actions.moveToElement(menuHoverLink);
        //driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
        WebElement subLink=driver.findElement(By.cssSelector(".options>ul>li>a"));
        actions.moveToElement(subLink);
        actions.click();
        actions.perform();

Since the menu on http://www.kgisliim.ac.in/ takes a second to slide out, you could add a WebDriverWait to make sure the submenu has time to become visible before moving the cursor to it. Try replacing the first line in your for loop with the following line. This will wait a maximum of 5 seconds for the submenu (but will return the WebElement as fast as possible within that time).

tempElem = new WebDriverWait(driver, 5).until(ExpectedConditions
        .elementToBeClickable(By.linkText(menuItems[i].trim())));

I stumbled across a similar issue recently, with phantomJS and ghostdriver . In my case, the problem was the window size - the HTML element was outside the visible area and my mouse movements were having no effect (default size is 400x300, which is rather small).

You can check the window size with

driver.manage().window().getSize()

And you can change it with

driver.manage().window().setSize(new Dimension(width, height));

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