java 使用 webdriver selenium 处理子菜单项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16160997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:04:20  来源:igfitidea点击:

handling submenu item with webdriver selenium

javaseleniumwebdriverselenium-webdriver

提问by Krishna Yadav

I want to click submenu item using selenium webdriver which is invisible bydefault. It becomes visible on mousehover . I tried with some code and it is giving error as shown below

我想使用默认情况下不可见的 selenium webdriver 单击子菜单项。它在 mousehover 上变得可见。我尝试了一些代码,它给出了如下所示的错误

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

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

Here the code:

这里的代码:

    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();    

回答by niharika_neo

Use the Actionsclass to do a mousehover on your menu item and then a click on the submenu option. You can refer to Actions class to get an overview of the methods available and a good help hereto understand how to use these interactions.

使用Actions类将鼠标悬停在菜单项上,然后单击子菜单选项。您可以参考 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();

I am hoping your locatros are correct..you might want to use a[contains(@href,'nemc.com/rentals')'

我希望你的定位是正确的..你可能想使用 [contains(@href,'nemc.com/rentals')'

回答by Hemanth

Try using the below code. It should work.Try adding perform() to your moveToElement statement as shown below.

尝试使用以下代码。它应该可以工作。尝试将 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();

回答by Manigandan

In some applications the Actioninteractions may not work. Personally i faced the problem and then i used the below solution. I took this solution from selenium issue tracker page.

在某些应用程序中,Action交互可能不起作用。我个人遇到了这个问题,然后我使用了以下解决方案。我从 selenium 问题跟踪器页面获取了这个解决方案。

 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;

回答by Neeme Praks

I stumbled across a similar issue recently, with phantomJSand ghostdriver. In my case, the problem was the window size - the HTML element was outside the visible area and my mouse movements were having no effect (default size is 400x300, which is rather small).

我最近偶然发现了一个类似的问题,phantomJSghostdriver。就我而言,问题在于窗口大小 - HTML 元素在可见区域之外,我的鼠标移动没有效果(默认大小为 400x300,相当小)。

You can check the window size with

您可以检查窗口大小

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

And you can change it with

你可以改变它

driver.manage().window().setSize(new Dimension(width, height));