有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在selenium中未获得鼠标悬停测试脚本的预期结果

下面是鼠标悬停在元素上的代码片段

public static void main(String[] args) throws InterruptedException
{
    System.setProperty("Webdriver.ie.driver","D://IEDriverServer.exe");
    WebDriver driver = new InternetExplorerDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.get("http://www.amity.edu/");

    String title = driver.getTitle();
    System.out.println(title);

    Actions action = new Actions(driver);
    WebElement MainTab = driver.findElement(By.xpath("//div[@class='main']/ul[2]/li[2]/a"));
    action.moveToElement(MainTab).build().perform();
}

共 (1) 个答案

  1. # 1 楼答案

    尝试下面的代码,它将在chromefirefox浏览器中工作

    由于IE浏览器存在一些问题,Action类无法使用IE Browser

    有关更多详细信息,请参阅此url

    System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");   //path for your browser.
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("http://www.amity.edu/");
    
    String page_title = driver.getTitle();
    System.out.println(page_title);
    
    WebElement main_menu = driver.findElement(By.xpath("//ul[@class='megamenu main-nav']/li/a[text()='Campuses']"));  // Get main menu
    WebElement child_menu = driver.findElement(By.xpath("//ul[@class='megamenu main-nav']//li/a[text()='Chennai']")); // get Submenu called Chennai 
    
    Actions act = new Actions(driver);
    act.moveToElement(main_menu).perform();
    new WebDriverWait(driver, 60).until(ExpectedConditions.visibilityOf(child_menu));     //wait for element
    act.moveToElement(child_menu).click().perform();