将鼠标悬停在菜单上并选择 java Selenium 中的子菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41666569/
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
Hover over menu and select sub menu in java Selenium
提问by Jerad Hobgood
I am trying to hover over a main menu and select a submenu using java selenium, i got it to hover over the menu but cant select the sub menu, if i try to find by linktext i always get the error "does not exist " if i use xpath the says build successful but does not open up the new page. Here is my code for it so far
我试图将鼠标悬停在主菜单上并使用 java selenium 选择一个子菜单,我让它悬停在菜单上但无法选择子菜单,如果我尝试通过链接文本查找我总是收到错误“不存在”如果我使用 xpath 说构建成功但没有打开新页面。到目前为止,这是我的代码
System.setProperty("webdriver.chrome.driver","C:/Driver/chromedriver.exe");
WebDriver webDriver = new ChromeDriver();
webDriver.manage().window().maximize();
webDriver.navigate().to("https://www.skiutah.com");
String NavTo = "DEALS";
String pathx = "//*[@id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a" ;
WebElement element = webDriver.findElement(By.linkText(NavTo));
WebElement el = webDriver.findElement(By.xpath(pathx));
Actions action = new Actions(webDriver);
action.moveToElement(element).perform();
action.moveToElement(el).click();
回答by Sameer Patil
In WebDriver we have given option to control Mouse
events. Try this piece of code. This should serve the purpose.
在 WebDriver 中,我们提供了控制Mouse
事件的选项。试试这段代码。这应该达到目的。
driver.get("https://www.skiutah.com/");
WebElement deals = driver.findElement(By.xpath("//a[@title='Deals']"));
Mouse mouse = ((HasInputDevices) driver).getMouse();
Locatable hoverItem = (Locatable) deals;
mouse.mouseMove(hoverItem.getCoordinates());
WebElement beginner = driver.findElement(By.xpath("//a[text()='Beginner']"));
new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOf(beginner));
Locatable clickItem = (Locatable) beginner;
mouse.mouseDown(clickItem.getCoordinates());
mouse.mouseUp(clickItem.getCoordinates());
System.out.println(driver.getTitle());
回答by amogh
To use mouse over action we need to use build.perform. It is called as action chaining which ensure that its perform actions together at the end. Or you can swap the line as below and it should work for you. I tried looks good.
要使用鼠标悬停操作,我们需要使用 build.perform。它被称为动作链,它确保它在最后一起执行动作。或者您可以按如下方式交换线路,它应该适合您。我试过看起来不错。
String NavTo = "DEALS";
String pathx = "//*[@id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a" ;
WebElement element = webDriver.findElement(By.linkText(NavTo));
WebElement el = webDriver.findElement(By.xpath(pathx));
Actions action = new Actions(webDriver);
action.moveToElement(el).click();
String NavTo = "DEALS";
String pathx = "//*[@id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a" ;
WebElement element = webDriver.findElement(By.linkText(NavTo));
WebElement el = webDriver.findElement(By.xpath(pathx));
Actions action = new Actions(webDriver);
action.moveToElement(el).click();
action.moveToElement(element).perform();
回答by Samir 007
//locate the menu to hover over using its xpath
WebElement menu = driver.findElement(By.linkText("Deals"));
//Initiate mouse action using Actions class
Actions builder = new Actions(driver);
// move the mouse to the earlier identified menu option
builder.moveToElement(menu).build().perform();
// wait for max of 5 seconds before proceeding.
// until this submenu is found
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a")));
//identify menu option from the resulting menu display and click
WebElement menuOption = driver.findElement(By.xpath("//*[@id=\"top_menu\"]/ul/li[4]/ul/li[1]/ul/li[2]/a"));
menuOption.click();
回答by Geomatani
This works for me first time, but if repeated for other menu item then it cant find or something.
WebElement menu = driver.findElement(By.your_locator);
WebElement sub_menu = driver.findElement(By.your_locator);
Actions action = new Actions(driver);
action.moveToElement(menu).moveToElement(sub_menu).click().build().perform();
回答by NarendraR
I think the way you are hovering and clicking on a sub-menu is not seems correct.
我认为您悬停和单击子菜单的方式似乎不正确。
You haven't shared your html
so its little bit tedious to check element you have located are correct or not. If all fine, Try following code which might help you -
你还没有分享你的html
所以检查你找到的元素是否正确有点乏味。如果一切正常,请尝试以下可能对您有所帮助的代码 -
WebElement menu = driver.findElement(By.your_locator);
WebElement sub_menu = driver.findElement(By.your_locator);
Actions action = new Actions(driver);
action.moveToElement(menu).moveToElement(sub_menu).click().build().perform();
Explaination :-
解释:-
Here build()
method is used to compile all the list of actions into a single step and ready to be performed
这里的build()
方法用于将所有动作列表编译为一个步骤并准备执行
回答by sridhar kasi
First Mousehover to the main menu and then click any of the submenus.
首先将鼠标悬停到主菜单,然后单击任何子菜单。
WebDriverWait Wait = new WebDriverWait(driver,10);
Wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//[@id='top_menu']/ul/li[4]/a"))));
Actions mousehover = new Actions(driver);
mousehover.moveToElement(driver.findElement(By.linkText("Deals"))).build().perform();
Wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.linkText("All Deals"))));
driver.findElement(By.linkText("All Deals")).click();
回答by enugu uday
I'm trying to 'click' the sub-menu. Need to mouse over on to the main menu and it takes few seconds to load the sub-menu. then i need to locate the sub-menu and click it. Here is the code which i used
我正在尝试“单击”子菜单。需要将鼠标悬停在主菜单上,加载子菜单需要几秒钟的时间。然后我需要找到子菜单并单击它。这是我使用的代码
Actions ac = new Actions(dr);
WebElement we = dr.findElement(By.xpath(".//*[@id='ddtopmenubar']/ul/li[1]/a"));
ac.moveToElement(we).build().perform();
WebDriverWait wait = new WebDriverWait(dr, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='dataingestionsubmenu']/li[2]/a")));
WebElement e= dr.findElement(By.xpath(".//*[@id='dataingestionsubmenu']/li[2]/a"));
e.click();
but it doesn't seems to work out.
但它似乎不起作用。
getting exception as : org.openqa.selenium.WebDriverException: performActions Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
获取异常为:org.openqa.selenium.WebDriverException:performActions 构建信息:版本:“未知”,修订:“未知”,时间:“未知”
when i do the same in debug mode, then i'm able to click submenu.
当我在调试模式下做同样的事情时,我就可以点击子菜单。