Hover over menu and select sub menu in java Selenium

六月ゝ 毕业季﹏ 提交于 2020-12-31 14:01:30

问题


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

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

回答1:


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



回答2:


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



回答3:


In WebDriver we have given option to control Mouse events. Try this piece of code. This should serve the purpose.

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



回答4:


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.

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



回答5:


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 -

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




回答6:


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



回答7:


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'

when i do the same in debug mode, then i'm able to click submenu.



来源:https://stackoverflow.com/questions/41666569/hover-over-menu-and-select-sub-menu-in-java-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!