简体   繁体   English

如何使用带有Java的Selenium WebDriver将鼠标悬停在Web元素上

[英]How to mouseover on a webelement using Selenium WebDriver with Java

How to perform a mouse hover functionality using Selenium Webdriver? 如何使用Selenium Webdriver执行鼠标悬停功能?

Test Case is like say, open Yahoo site and there is link (Mail) beside Sign-In. 测试用例就像说,打开Yahoo网站,登录旁边有链接(邮件)。 Upon mouse hover it will show a tooltip. 鼠标悬停时将显示工具提示。

When i try the below code, it is not mouse hovering the exact location, rather it hovers somewhere else. 当我尝试以下代码时,不是鼠标悬停在确切位置,而是将鼠标悬停在其他位置。 Where i am going wrong? 我要去哪里错了?

And also let me know, how to capture the Tooltip? 另外,也请告诉我,如何捕获工具提示?

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;


public class Sample 
{
    public static void main(String[] args) 
    {
        WebDriver driver=new FirefoxDriver();
        driver.get("http://www.yahoo.com");

        driver.manage().window().maximize();

        try 
                {
            Thread.sleep(5000);
        } catch (InterruptedException e)
                {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        WebElement lMail=driver.findElement(By.xpath("//*[@title='Mail']"));

        Actions builder=new Actions(driver);
        builder.moveToElement(lMail).build().perform();


    }

}
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();

Try this code: 试试这个代码:

//Assume driver initialized properly.
WebElement element = driver.findElement(By.id("Element id"));
Locatable hoverItem = (Locatable) element;
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseMove(hoverItem.getCoordinates());

I have used similar code and it works for me. 我使用过类似的代码,它对我有用。 I have also used the following at a few places: browser.executeScript("jQuery('mycss-selector').mouseover();") You will have to use css-selector, instead of xpath. 我还在一些地方使用了以下代码:browser.executeScript(“ jQuery('mycss-selector')。mouseover();”)您将不得不使用css-selector而不是xpath。

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

相关问题 如何使用Java和Selenium WebDriver识别动态WebElement上传图片 - How to identify dynamic WebElement to upload the picture using Java and Selenium WebDriver 使用带有Java的Selenium Webdriver滚动Webelement - Scrolling Webelement using Selenium Webdriver with Java 使用 Selenium WebDriver 和 java 断言 WebElement 不存在 - Assert that a WebElement is not present using Selenium WebDriver with java 如何不使用javascriptExecutor在Selenium Webdriver中突出显示Webelement - How to highlight webelement in selenium webdriver not using javascriptExecutor 如何使用 Java 在 Selenium WebDriver 中执行鼠标悬停功能? - How to perform mouseover function in Selenium WebDriver using Java? 使用带有 Java 的 Selenium WebDriver 在新页面上查找 WebElement - Find a WebElement on the new page using Selenium WebDriver with Java 如何通过Selenium WebDriver Java单击覆盖标记下的Webelement - how to click webelement under overlay tag through selenium webdriver java 如何在Selenium Webdriver(Java)中从Web元素获取数值 - How to get a numeric value from a webelement in Selenium Webdriver (Java) 如何将鼠标指针移到WebElement上 - How to move mouse pointer onto a webelement is selenium webdriver java 如何使用 Selenium Webdriver 测试 WebElement 属性是否存在 - How to test if a WebElement Attribute exists using Selenium Webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM