如何使用Java在Selenium WebDriver中执行鼠标悬停功能?
我想在下拉菜单上执行鼠标悬停功能。当我们将鼠标悬停在菜单上时,它将显示新的选项。 我尝试使用xpath单击新选项。但不能直接单击菜单。 因此,作为手动方式,我尝试将鼠标悬停在下拉菜单上,然后单击新选项如何使用Java在Selenium WebDriver中执行鼠标悬停功能?,java,selenium,selenium-webdriver,mouseover,Java,Selenium,Selenium Webdriver,Mouseover,我想在下拉菜单上执行鼠标悬停功能。当我们将鼠标悬停在菜单上时,它将显示新的选项。 我尝试使用xpath单击新选项。但不能直接单击菜单。 因此,作为手动方式,我尝试将鼠标悬停在下拉菜单上,然后单击新选项 Actions action = new Actions(webdriver); WebElement we = webdriver.findElement(By.xpath("//html/body/div[13]/ul/li[4]/a")); action.moveToElement(we).
Actions action = new Actions(webdriver);
WebElement we = webdriver.findElement(By.xpath("//html/body/div[13]/ul/li[4]/a"));
action.moveToElement(we).build().perform();
执行“鼠标悬停”操作实际上是不可能的,相反,您需要一次完成所有要执行的操作。因此,移动到显示其他元素的元素,然后在同一链中,移动到现在显示的元素并单击它 当使用动作链时,你必须记住“像用户那样做”
Actions action = new Actions(webdriver);
WebElement we = webdriver.findElement(By.xpath("html/body/div[13]/ul/li[4]/a"));
action.moveToElement(we).moveToElement(webdriver.findElement(By.xpath("/expression-here"))).click().build().perform();
根据博客文章,我能够使用Selenium 2 Webdriver的以下代码触发悬停:
String javaScript = "var evObj = document.createEvent('MouseEvents');" +
"evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" +
"arguments[0].dispatchEvent(evObj);";
((JavascriptExecutor)driver).executeScript(javaScript, webElement);
尝试执行以下操作时,这些答案均无效:
Actions builder = new Actions(driver);
builder.moveToElement(hoverElement).perform();
builder.moveToElement(clickElement).click().perform();
这对我不起作用。以下几点对我很有用:
Actions builder = new Actions(driver);
builder.moveToElement(hoverElement).perform();
By locator = By.id("clickElementID");
driver.click(locator);
使用悬停操作和标准WebDriver单击,我可以悬停然后单击。我发现这个问题是为了寻找一种方法,使用量角器(Selenium的Javascript前端)对我的Javascript测试执行相同的操作 使用量角器1.2.0和webdriver 2.1的解决方案:
browser.actions()
.mouseMove(
element(by.css('.material-dialog-container'))
)
.click()
.perform();
这也接受一个偏移量(我用它来单击元素的左上方:)
使用Selenium java WebDriver进行鼠标悬停的示例程序:
public class Mhover {
public static void main(String[] args){
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.google.com");
WebElement ele = driver.findElement(By.id("gbqfba"));
Actions action = new Actions(driver);
action.moveToElement(ele).build().perform();
}
}
此代码运行良好:
Actions builder = new Actions(driver);
WebElement element = driver.findElement(By.linkText("Put your text here"));
builder.moveToElement(element).build().perform();
鼠标悬停后,您可以继续对显示的信息执行下一步操作检查此示例我们如何实现此操作
public类hoverabledropdownstest{
私有网络驱动程序;
私人行动;
//编辑:“->”表达式中可能有输入错误(我不想添加此评论,但坚持“>6个字符编辑”。。。
消费者hover=(By)->{
action.moveToElement(driver.findelelement(by))
.perform();
};
@试验
公开测试(){
驱动程序。获取(“https://www.bootply.com/render/6FC76YQ4Nh");
hover.accept(通过.linkText(“下拉”);
hover.accept(By.linkText(“下拉链接5”);
hover.accept(按.linkText(“下拉子菜单链接5.4”);
hover.accept(按.linkText(“下拉子菜单链接5.4.1”);
}
@试验前
公共无效设置驱动程序(){
驱动程序=新的FirefoxDriver();
动作=新动作(驱动程序);
}
@事后
public void拆卸驱动程序(){
driver.quit();
}
}
有关详细答案,请点击此处-您可以尝试:
WebElement getmenu= driver.findElement(By.xpath("//*[@id='ui-id-2']/span[2]")); //xpath the parent
Actions act = new Actions(driver);
act.moveToElement(getmenu).perform();
Thread.sleep(3000);
WebElement clickElement= driver.findElement(By.linkText("Sofa L"));//xpath the child
act.moveToElement(clickElement).click().perform();
如果你有一个网站有很多类别,使用第一种方法。对于你想要的菜单,你只需要第二种方法。我试过了,效果正常
action = ActionChains(driver)
element = driver.find_element_by_xpath("XPath_selector")
action.move_to_element(element).perform()
对我来说,这不起作用。我的菜单只有在我在moveToElement()之后执行build().perform()时才会悬停。这不起作用的原因是对
webdriver.findelelement(By…something)
的所有调用都是在执行其他任何操作之前执行的(这是将结果传递给moveElement
)。此时,您要查找的第二个元素尚不可见,因为第一个元素仍必须悬停在上方。要解决此问题,如您所说,可以插入intermediate.perform() S,然后对于第二个<代码> FixEngult<代码>,第一个悬停将是“代码>执行< /代码> ED。给定的解决方案可能会工作,这取决于页面的实现,但显然您和我的里程有所不同。请考虑包括关于您的答案的一些信息,而不是简单地张贴代码。我们尝试提供不是JUS。“修复”,但帮助人们学习。您应该解释原始代码中的错误,您的不同之处,以及更改的原因工作。@AndrewBarber-给定的程序确实可以帮助用户。该程序工作正常。用户已经接受了。我不怀疑它会工作;我是说你应该解释为什么它会工作,为什么他们没有工作,以及你做了什么更改。这段代码相当于OP,并且没有回答问题。没有任何c文本信息是多余的。第二个示例在添加.perform()时对我也起了作用。我不敢相信这仍然是一个问题…甚至这都不起作用:builder.moveToElement(设置)。moveByOffset(0,30)。moveToElement(stagingMenu)。pause(20000)。Keys.CONTROL)。单击(stagingMenu)。键入(Keys.CONTROL)。发送键(Keys.ENTER)。执行();我甚至在超时范围内看到元素上的悬停css触发器。但是,无论我尝试什么,都不会触发click。如果clickable不是一个普通元素,它像::before,您将如何处理。当您将鼠标悬停在不太明显的解决方案上时,这一触发器是可见的,但对于我的IE11测试,它是100%可靠的。如果您在悬停时遇到问题moveToElement
,使用这个!我用C#编写代码,所以这不是Java唯一的方法。Selenium&,Javascript这是什么参数[0]
?@ArianHosseinzadeh是传递给executeScript()的第二个参数的传入dom引用
,这是一个Web元素
我得到了Javascript executor的missign ref。我需要在C中添加什么参考资料?查看此站点以获得详细答案-优秀的只是需要使用OpenQA.Selenium.Interactions添加。
WebElement getmenu= driver.findElement(By.xpath("//*[@id='ui-id-2']/span[2]")); //xpath the parent
Actions act = new Actions(driver);
act.moveToElement(getmenu).perform();
Thread.sleep(3000);
WebElement clickElement= driver.findElement(By.linkText("Sofa L"));//xpath the child
act.moveToElement(clickElement).click().perform();
action = ActionChains(driver)
element = driver.find_element_by_xpath("XPath_selector")
action.move_to_element(element).perform()