2013-04-23 53 views
0

我想用selenium webdriver点击子菜单项,这是默认不可见的。 它在mousehover上可见。 我试着用一些代码,如下图所示用webdriver selenium处理子菜单项

Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Element is not currently visible and so may not be interacted with.

下面的代码是给错误:

Actions actions = new Actions(driver); 
    WebElement menuHoverLink = driver.findElement(By.linkText("RENT")); 
    //WebElement menuHoverLink = driver.findElement(By.className("current")); 
    actions.moveToElement(menuHoverLink); 
    WebElement subLink = driver.findElement(By.cssSelector("a[href='nemc.com/rentals/easy-rent']")); 
    actions.moveToElement(subLink); 
    actions.click(); 
    actions.perform();  
+0

您的鼠标悬停逻辑有问题改变它。请向我们展示您的代码? – Hemanth 2013-04-23 04:48:52

+0

我已经使用了下面的代码,它正在工作,但问题是它重定向到其他菜单项:操作actions = new Actions(driver); \t WebElement menuHoverLink = driver.findElement(By.linkText(“RENT”)); \t // WebElement menuHoverLink = driver.findElement(By.className(“current”)); \t actions.moveToElement(menuHoverLink); \t \t WebElement subLink = driver.findElement(By.cssSelector(“a [href ='http://www.nemc.com/rentals/easy-rent']”)); \t actions.moveToElement(子链路); \t actions.click(); \t actions.perform(); – 2013-04-23 06:38:30

+0

编辑我的答案..如果出来..如果它的网站你可以分享..然后分享网址.. – 2013-04-23 07:32:50

回答

2

使用Actions类做你的菜单项的mousehover,然后点击在子菜单选项上。您可以参考Actions类以获得可用方法的概述,并了解如何使用这些交互。

Actions actions = new Actions(driver); WebElement menuHoverLink = driver.findElement(By.linkText("RENT")); 
actions.moveToElement(menuHoverLink).perform(); 
driver.findElement(By.cssSelector("a[href='nemc.com/rentals/easy-rent']")).click(); 

我希望你locatros是correct..you可能希望使用[包含(@ HREF, 'nemc.com /租赁')”

0

尝试使用下面的代码。它应该工作。尝试将perform()添加到moveToElement语句中,如下所示。

Actions actions = new Actions(driver); 
WebElement menuHoverLink = driver.findElement(By.linkText("RENT")); 

actions.moveToElement(menuHoverLink).perform(); 
WebElement subLink = driver.findElement(By.cssSelector("a[href='nemc.com/rentals/easy-rent']")); 
sublink.click(); 
0

在某些应用程序中,Action交互可能不起作用。我个人面临的问题,然后我使用下面的解决方案。我从硒问题跟踪器页面采取了此解决方案。

WebElement targetElement = driver.findElement(By.id("locator")); 
JavascriptExecutor js = (JavascriptExecutor) driver; 
String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}"; 
js.executeScript(mouseOverScript, targetElement); 
driver.findElement(By.id("Click locator")).click; 
0

我遇到了类似的问题跌跌撞撞最近,随着phantomJSghostdriver。在我的情况下,问题在于窗口大小 - HTML元素超出了可见区域,我的鼠标移动没有任何效果(默认大小为400x300,这相当小)。

您可以

driver.manage().window().getSize() 

检查窗口的大小和你可以用

driver.manage().window().setSize(new Dimension(width, height)); 
相关问题