![](/img/trans.png)
[英]Mouse hover not working for Firefox browser using Selenium webdriver
[英]How to do mouse hover using Selenium WebDriver in Firefox 19?
我使用了硒 2.31。
我已经使用 Actions 类进行鼠标移动。 使用这个,我将鼠标移到一个菜单上,它的子菜单只出现了几分之一秒,与旧版本的 Firefox 不同。
由于这个问题,我无法使用driver.findElement
选择子菜单,因为它会引发异常“元素无法滚动到视图中”。
有什么解决办法吗?
对于动作对象,您应该首先移动菜单标题,然后移动到弹出菜单项并单击它。 最后不要忘记调用actions.perform()
。 下面是一些示例 Java 代码:
Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.linkText("Menu heading"));
actions.moveToElement(menuHoverLink);
WebElement subLink = driver.findElement(By.cssSelector("#headerMenu .subLink"));
actions.moveToElement(subLink);
actions.click();
actions.perform();
另一种方法是使用 Selenium 的 JavaScript Executor 来强制显示元素的样式。
这方面的一个例子将在 C# 中沿着这条线
//Use the Browser to change the display of the element to be shown
(IJavaScriptExecutor)driver).ExecuteScript("document.getElementById('myId').stlye.display="block");
//navigate to your link that is now viewable
driver.FindElement(By.Xpath('//LinkPath')).Click();
从那里,您可以找到元素的 XPath 并使用 selenium 单击该元素。 您也可以级联它以查找主要元素的子元素
//(IJavaScriptExecutor)ffbrowser).ExecuteScript("document.getElementById('myId').children[1].children[1].style.display='block'");
请注意,只有当您有一个悬停元素可以在悬停时更改显示样式时,这才有可能。
试试这个代码......这是一个清晰的代码......
//Webelement is the main menu Link
webElement = driver.FindElement(By.XPath("Your element xpath"));
Actions act = new Actions(driver);
act.MoveToElement(webElement).Perform();//This opens menu list
System.Threading.Thread.Sleep(5000);//This line will help you to hold menu
//This web element is the sub menu which is under main menu
webElement = driver.FindElement(By.XPath("Sub menu path"));
act.MoveToElement(webElement).Perform();//This opens menu list
System.Threading.Thread.Sleep(5000);//Holds menu
//This web element is the option you have to click
webElement = driver.FindElement(By.XPath("Path"));
webElement.Click();
如果您使用 Ruby,这将很有帮助。
1.首先你需要通过xpath或者id来查找元素。
2.然后使用方法action.move_to().perform。
这是代码:
hover = WAIT.until{$driver.find_element(:xpath,"xpath")}
driver.action.move_to(hover).perform
这个答案帮助解决了我的问题。
我的挑战是在菜单选项下找到一个链接。 在我将鼠标悬停在菜单上之前,该链接不可见。
对我来说,这一关键部分是发现除了将鼠标悬停在菜单上之外,接下来我还必须将鼠标悬停在链接上才能与其进行交互。
List<WebElement> list = driver.findElements(By.xpath("//a"));
for (int i=0;i<list.size();i++){
if(list.get(i).getText().equalsIgnoreCase("cacique intimates M"))
{
new Actions(driver).moveToElement(list.get(i)).click().build().perform();
System.out.println("Clicked on Parent Category");
new Actions(driver).moveToElement(list.get(i)).moveToElement(driver.findElement(By.linkText("SPECIALTY BRAS"))).click().build().perform();
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.