简体   繁体   English

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

[英]How to mouse hover and click on element in webdriver

I need to mouse hovering on one element but i am not able to mousehover it.First i need to mouse hover on one element then i need to select another one.我需要将鼠标悬停在一个元素上,但我无法将鼠标悬停在它上面。首先我需要将鼠标悬停在一个元素上,然后我需要选择另一个元素。 I need to mouse hovering on one element but i am not able to mousehover it.First i need to mouse hover on one element then i need to select another one.我需要将鼠标悬停在一个元素上,但我无法将鼠标悬停在它上面。首先我需要将鼠标悬停在一个元素上,然后我需要选择另一个元素。 I need to mouse hovering on one element but i am not able to mousehover it.First i need to mouse hover on one element then i need to select another one.我需要将鼠标悬停在一个元素上,但我无法将鼠标悬停在它上面。首先我需要将鼠标悬停在一个元素上,然后我需要选择另一个元素。 I need to mouse hovering on one element but i am not able to mousehover it.First i need to mouse hover on one element then i need to select another one.我需要将鼠标悬停在一个元素上,但我无法将鼠标悬停在它上面。首先我需要将鼠标悬停在一个元素上,然后我需要选择另一个元素。 package nxusdata;包 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();


    }



    }
}

You can try something like this: 你可以尝试这样的事情:

Actions actions = new Actions(driver);
WebElement menu = driver.findElement(By.xpath("//*[@id='menu-links']/a/div[3]"));
actions.moveToElement(menu);

WebElement subMenu = driver.findElement(By.xpath("//*[@id='menu-links']/ul/li[2]/a"));
actions.moveToElement(subMenu);
actions.click().build().perform();

Learn more here . 在这里了解更多。

    // Locating the Main Menu (Parent element)
    WebElement mouseHoverOnLocations = driver.findElement(By.partialLinkText("Locations"));

    // Instantiating Actions class
    Actions actions = new Actions(driver);

    // Hovering on main menu
    actions.moveToElement(mouseHoverOnLocations);

    // Locating the element from Sub Menu
    WebElement selectSantaClara = driver.findElement(By.linkText("Santa Clara"));

    // To mouseover on sub menu
    actions.moveToElement(selectSantaClara);

    // build()- used to compile all the actions into a single step
    actions.click().build().perform();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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