【发布时间】:2013-01-19 11:58:24
【问题描述】:
如何使用 Selenium Webdriver 执行鼠标悬停功能?
测试用例就像说,打开雅虎网站,登录旁边有链接(邮件)。 鼠标悬停时,它会显示一个工具提示。
当我尝试下面的代码时,它不是鼠标悬停在确切的位置,而是悬停在其他地方。我哪里出错了?
还请告诉我,如何捕获工具提示?
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();
}
}
【问题讨论】:
-
代码看起来很正常,你看到它悬停在哪里?
-
我同意 Ardesco,代码看起来正确。我看到你和我使用的唯一区别是我不会在
perform()之前调用build()。所以我最后的电话是这样的:new Actions(driver).moveToElement(element).perform();