如何鼠标悬停并单击webdriver中的元素

时间:2016-03-11 14:00:49

标签: java selenium

我需要将鼠标悬停在一个元素上,但我无法将鼠标悬停在它上面。首先我需要将鼠标悬停在一个元素上,然后我需要选择另一个元素。   我需要鼠标悬停在一个元素上,但我无法鼠标悬停它。首先我需要鼠标悬停在一个元素上然后我需要选择另一个元素。   我需要鼠标悬停在一个元素上,但我无法鼠标悬停它。首先我需要鼠标悬停在一个元素上然后我需要选择另一个元素。   我需要鼠标悬停在一个元素上,但我无法鼠标悬停它。首先我需要鼠标悬停在一个元素上然后我需要选择另一个元素。      包nxusdata;

import java.util.Iterator;
import java.util.Set;
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.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class MouseHoveringworpress {
    public static WebDriver driver;

    @BeforeTest
    public void openinBrowser(){


        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
         driver = new ChromeDriver();
        driver.get("https://wordpress.com/wp-login.php?redirect_to=https%3A%2F%2Fwordpress.com%2F");

        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    }



    @Test
    public void menus() throws InterruptedException{

        driver.findElement(By.xpath("//*[@id='user_login']")).sendKeys("ritu7180");
        driver.findElement(By.xpath("//*[@id='user_pass']")).sendKeys("jaiguruji123");
        driver.findElement(By.xpath("//*[@id='wp-submit']")).click();
        //*[@id='header']/a[2]/span


        Thread.sleep(15000);
        driver.findElement(By.xpath("//*[@id='header']/a[1]/span")).click();




        //*[@id='secondary']/div/ul/li[4]/ul/li[5]/a/span[1]
        Thread.sleep(4000);


        driver.findElement(By.xpath("//*[@id='secondary']/div/ul/li[4]/ul/li[5]/a/span[1]")).click();
        Thread.sleep(10000);
        Set <String> windows =driver.getWindowHandles();
        System.out.println(windows.size());
        //Print the size of windows
        Iterator<String> it = windows.iterator();
        //iterate through your windows
        while (it.hasNext()){ 
        String parentwindow = it.next();
        System.out.println("This is first window id  "+parentwindow);
        String childwindow = it.next();
        System.out.println("this is second window id  "+childwindow);

        driver.switchTo().window(childwindow);
    //  driver.findElement(By.xpath("//*[@id='save-post']")).click();

        Thread.sleep(4000);


        WebElement menu = driver.findElement(By.xpath("//*[@id='menu-links']/a/div[3]"));
        WebElement SUBMenu   = driver.findElement(By.xpath("//*[@id='menu-links']/ul/li[2]/a"));

        Actions action =    new Actions(driver);
        action.moveToElement(menu).perform();
        Thread.sleep(2000);
        action.click(SUBMenu).perform();


    }



    }
}

1 个答案:

答案 0 :(得分:3)

您可以尝试这样的事情:

<a href

了解详情here