0

I am trying to dynamically select items from a drop down menu based on input. This isn't really a problem unless values share a name. For instance if this is my Cucumber statement

 When I go to the "Inventory" / "Inventory" application

When I call this method:

@When("^I go to the \"([^\"]*)\" / \"([^\"]*)\" application$")
public void i_go_to_the_application(String hoverMenu, String subMenuName) throws Throwable {
        Actions actions = new Actions(driver);
    WebElement menuHoverLink = driver.findElement(By.linkText(hoverMenu));
    actions.moveToElement(menuHoverLink);
    Thread.sleep(1000);
    WebElement submenu = driver.findElement(By.xpath("//span[.='" + subMenuName + "']"));

    actions.moveToElement(submenu);
    actions.click().perform();
    Thread.sleep(4000);

My test crash because it cannot find the subMenuElement. This code works swimmingly if the menu names are different such as Inventory > Items.

Now I've already checked this question Selenium: handling multiple inputs with same xpath

However the solution provide doesn't seem to be helpful since both elements are in the drop down menu with one being a submenu to that one: Inventory > Inventory

I ran this through the selenium IDE and no matter how many ways I've tried to "find" the Inventory element it only ever finds one item. Since the user of my tests can select any number of items from a drop down menu and then subsequent submenus, I can't rely on IDs. Is there a way to select the first menu item for sure and then select the submenu item.

1
  • Could you share HTML for this Menu and submemu as well Commented Sep 2, 2016 at 18:47

2 Answers 2

1

Change this from a partial link text to a xpath search

WebElement menuHoverLink = driver.findElement(By.linkText(hoverMenu));

to below

WebElement menuHoverLink = driver.findElement("//a[.='" + hoverMenu + "']");

You just need the 2 xpaths to be different so even if the names are same you will get your result.

Sign up to request clarification or add additional context in comments.

1 Comment

I've tried this approach, both times they find the same element. I've used a slight work around where I scan for the elements ID and then use a !comparison to find the other element.
0

It seems submemu Inventory element existing inside main menu Inventory element. Then you should try to find submemu element on the main menu context as below :-

Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.linkText(hoverMenu));
actions.moveToElement(menuHoverLink);

WebElement submenu = new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfNestedElementLocatedBy(menuHoverLink, By.xpath(".//span[.='" + subMenuName + "']")))

actions.moveToElement(submenu);
actions.click().perform();

2 Comments

This still returns the top level item.
Tried once as menuHoverLink.findElement(By.xpath(".//span[.='" + subMenuName + "']")); and let me know..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.