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

Java 如何在webdriver中鼠标悬停并单击元素,java,selenium,Java,Selenium,我需要将鼠标悬停在一个元素上,但我无法将鼠标悬停在它上面。首先我需要将鼠标悬停在一个元素上,然后我需要选择另一个元素。 我需要将鼠标悬停在一个元素上,但我无法将鼠标悬停在它上面。首先我需要将鼠标悬停在一个元素上,然后我需要选择另一个元素。 我需要将鼠标悬停在一个元素上,但我无法将鼠标悬停在它上面。首先我需要将鼠标悬停在一个元素上,然后我需要选择另一个元素。 我需要将鼠标悬停在一个元素上,但我无法将鼠标悬停在它上面。首先我需要将鼠标悬停在一个元素上,然后我需要选择另一个元素。 包装数据 impor

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

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();


    }



    }
}
import java.util.Iterator;
导入java.util.Set;
导入java.util.concurrent.TimeUnit;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.chrome.ChromeDriver;
导入org.openqa.selenium.interactions.Action;
导入org.openqa.selenium.interactions.Actions;
导入org.testng.annotations.BeforeTest;
导入org.testng.annotations.Test;
公共类鼠标悬停Worpress{
公共静态WebDriver;
@试验前
public void openinBrowser(){
System.setProperty(“webdriver.chrome.driver”、“chromedriver.exe”);
驱动程序=新的ChromeDriver();
驱动程序。获取(“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);
}
@试验
public void menus()引发InterruptedException{
driver.findelelement(By.xpath(“/*[@id='user_login'])).sendKeys(“ritu7180”);
findElement(By.xpath(“//*[@id='user_pass'])).sendKeys(“jaiguruji123”);
driver.findElement(By.xpath(“/*[@id='wp-submit']”)。单击();
//*[@id='header']/a[2]/span
睡眠(15000);
findelelement(By.xpath(“//*[@id='header']/a[1]/span”)。单击();
//*[id='secondary']/div/ul/li[4]/ul/li[5]/a/span[1]
睡眠(4000);
findelelement(By.xpath(“//*[@id='secondary']/div/ul/li[4]/ul/li[5]/a/span[1]”)。单击();
睡眠(10000);
设置windows=driver.getWindowHandles();
System.out.println(windows.size());
//打印窗口的大小
Iterator it=windows.Iterator();
//遍历你的窗口
而(it.hasNext()){
字符串parentwindow=it.next();
System.out.println(“这是第一个窗口id”+父窗口);
String childwindow=it.next();
System.out.println(“这是第二个窗口id”+childwindow);
driver.switchTo().window(childwindow);
//driver.findElement(By.xpath(“/*[@id='save-post']))。单击();
睡眠(4000);
WebElement menu=driver.findElement(By.xpath(“/*[@id='menu-links']]/a/div[3]”);
WebElement子菜单=driver.findElement(By.xpath(“/*[@id='menu-links']]/ul/li[2]/a”);
动作动作=新动作(驱动);
action.moveToElement(菜单).perform();
《睡眠》(2000年);
操作。单击(子菜单)。执行();
}
}
}

您可以尝试以下方法:

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();

了解更多信息。

请详细提问。您已复制粘贴同一行两次或三次。。。。请参考此链接,反复复制粘贴同一行问题。我尝试过此链接,但它在该链接上突出显示,但没有打开其他选项。它应该单击。尝试使用显式等待而不是
thread.sleep
。WebDriverWait wait=新的WebDriverWait(驱动程序,10);WebElement=wait.until(ExpectedConditions.elementtobelickable(By.id(“someid”));它起作用了,但我使用了WebElement menu=driver.findElement(By.xpath(“//*[@id='menu-links']]/a/div[3]”);动作动作=新动作(驱动);act.moveToElement(菜单).perform();动作动作=新动作(驱动);actions.moveToElement(菜单)。单击并按住();WebElement子菜单=driver.findElement(By.xpath(“/*[@id='menu-links']]/ul/li[2]/a”);actions.moveToElement(子菜单);操作。单击().build().perform();