如何使用SeleniumWebDriver和Java在webelement上进行鼠标悬停
如何使用SeleniumWebDriver执行鼠标悬停功能 测试用例就像是说,打开雅虎网站,在登录旁边有一个链接(邮件)。 鼠标悬停时将显示工具提示 当我尝试下面的代码时,它不是鼠标悬停在确切的位置,而是在其他地方。我错在哪里 同时让我知道,如何捕捉工具提示如何使用SeleniumWebDriver和Java在webelement上进行鼠标悬停,java,selenium-webdriver,Java,Selenium Webdriver,如何使用SeleniumWebDriver执行鼠标悬停功能 测试用例就像是说,打开雅虎网站,在登录旁边有一个链接(邮件)。 鼠标悬停时将显示工具提示 当我尝试下面的代码时,它不是鼠标悬停在确切的位置,而是在其他地方。我错在哪里 同时让我知道,如何捕捉工具提示 import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.o
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();
}
}
尝试以下代码:
//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());
我使用了类似的代码,它对我很有用。 我也在一些地方使用了以下内容: executeScript(“jQuery('mycss-selector').mouseover();”)
您必须使用css选择器,而不是xpath。代码看起来很正常,您看到它停在哪里了?我同意Ardesco,代码看起来是正确的。我看到您的和我使用的唯一区别是,我在
perform()
之前没有调用build()
。因此,我的最后一个调用如下:newactions(driver).moveToElement(element.perform()代码>我在这里看到一些奇怪的行为。当我在WinXP机器上运行相同的代码时,鼠标悬停在对象上,但在Win7中,鼠标悬停在不同的位置。在上述两个代码中,它只是在光标移动(指向不同的随机位置)后将鼠标悬停在链接上。为什么呢?这里有人可以帮忙吗?在XP机器上运行时,您可以看到工具提示
文本(在您的情况下是邮件
)?
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();