首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将鼠标悬停并单击webdriver中的元素

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

Stack Overflow用户
提问于 2016-03-11 22:00:49
回答 2查看 27.4K关注 0票数 2

我需要鼠标悬停在一个元素上,但我不能鼠标悬停在it.First上,我需要将鼠标悬停在一个元素上,然后我需要选择另一个。我需要鼠标悬停在一个元素上,但我不能鼠标悬停在it.First上,我需要将鼠标悬停在一个元素上,然后我需要选择另一个。我需要鼠标悬停在一个元素上,但我不能鼠标悬停在it.First上,我需要将鼠标悬停在一个元素上,然后我需要选择另一个。我需要鼠标悬停在一个元素上,但我不能鼠标悬停在it.First上,我需要将鼠标悬停在一个元素上,然后我需要选择另一个。nxusdata包;

代码语言:javascript
复制
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();


    }



    }
}
EN

回答 2

Stack Overflow用户

发布于 2016-03-11 22:22:27

您可以尝试如下所示:

代码语言:javascript
复制
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();

了解更多here

票数 5
EN

Stack Overflow用户

发布于 2021-09-07 10:13:46

代码语言:javascript
复制
    // 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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35942196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档