[英]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.