鼠标悬停时无法按住某个元素

Unable to hold on an element on mouse hover

有一个鼠标悬停下拉菜单,悬停时下拉菜单出现,如果鼠标指针离开下拉菜单则消失。我尝试使用 "Actions" class 将鼠标悬停在下拉元素上,但在执行鼠标悬停后我看不到下拉菜单。在执行下一个操作之前,下拉列表不会停留。

有什么方法可以在执行悬停后等待,以便下拉菜单显示更长时间,以便从下拉菜单中选择 select/click 个元素?

代码:

driver= new ChromeDriver();
driver.get("w3schools.com/howto/howto_css_dropdown.asp");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
WebElement element = driver.findElement(By.xpath(".//*@id='main']/div[3]/button")‌​);
Actions action= new Actions(driver); 
action.moveToElement(element).perform();
Thread.sleep(5000);

环境:

Chrome version: 56.0.2924.87 (64-bit)
Chrome driver: 2.27

使用您为鼠标悬停操作编写的相同代码,并尝试在鼠标悬停操作之前执行以下解决方案。

  • 将鼠标光标保持在任务栏上。

  • 将鼠标光标移动到 (0, 0) 浏览器位置。

perform()后需要遍历元素

这是工作代码:

package demo;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class MouseHoverDemo_w3school {

    public static void main(String[] args) {



        System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.w3schools.com/howto/howto_css_dropdown.asp");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        WebElement ele = driver.findElement(By.xpath(".//div[@class='dropdown dropdown2']/button"));
        Actions act = new Actions(driver);
        act.moveToElement(ele).perform();
        List<WebElement> links = driver.findElements(By.xpath(".//div[@class='dropdown-content']/a"));
        int total_count = links.size();
        for (int i=0; i<total_count; i++)
        {
            WebElement element = links.get(i);
            String text = element.getAttribute("innerHTML");
            System.out.println("Link Name is : "+text);

        }

        driver.quit();
    }

}

如果这回答了您的问题,请告诉我。