Lior p
Lior p

Reputation: 45

How to select an item from menu-popup with selenium?

How can i select an element from menu-popup? For example i want to choose Mr.

This is the site code:

<div class="menu-popup-items"><span class="menu-popup-item menu-popup-no-icon "><span class="menu-popup-item-icon"></span><span class="menu-popup-item-text">Not selected</span></span><span class="menu-popup-item menu-popup-no-icon "><span class="menu-popup-item-icon"></span><span class="menu-popup-item-text">**Mr.**</span></span><span class="menu-popup-item menu-popup-no-icon "><span class="menu-popup-item-icon"></span><span class="menu-popup-item-text">Mrs.</span></span><span class="menu-popup-item menu-popup-no-icon "><span class="menu-popup-item-icon"></span><span class="menu-popup-item-text">Ms.</span></span><span class="menu-popup-item menu-popup-no-icon "><span class="menu-popup-item-icon"></span><span class="menu-popup-item-text">Dr.</span></span></div>

Upvotes: 0

Views: 2743

Answers (3)

akshay patil
akshay patil

Reputation: 688

I Will explain with my example

      //In Page Object File
      public static WebElement idProof(WebDriver driver)
        {
            WebElement element=null;
            WebDriverWait wait=new WebDriverWait(driver, 50);
            element=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='panel-body']//div[3]//div[2]//div[1]//a[1]//span[1]")));
            return element;
        }

     ` `//In Test File
      WebElement idProof = Page1.idProof(driver);
      idProof.click();

       //In Test File
       WebElement voterId = FarmerRegPage.idProofVoterId(driver, "Voter id");
       voterId.click();

  // In Page Object File
  public static WebElement idProofVoterId(WebDriver driver, String idVal)
{
    WebElement element=null;
    WebDriverWait wait=new WebDriverWait(driver, 50);
    element=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[contains(text(),'" + idVal +"')]")));
    return element;
}

variable String idVal is the value that i am passing at dropdown

HTML Snippet :- <span>Select an Option</span>

We have same situation and i Would like to tell you that you should click first on dropdown and after that you just change in xpath span to li and remaining as it is, send the name of the item you have to select this should work fine

Upvotes: 0

Yash Patel
Yash Patel

Reputation: 161

I think you should try with this XPath

"//*[@class='menu-popup-items']" 

it will help you to locate each and every element into your div tag

And if you are looking for specific text then it will help you to find an element on menu pop-up

 //*[contains(text(),'Mr.')]

It will locate your Mr on menu pop-up

Upvotes: 2

Thibstars
Thibstars

Reputation: 1083

Before answering the question, I would like to note that if you can add an id per menu item, it would make the lookup way easier and more performant.

In this scenario you could then do:

WebElement result = driver.findElement(By.id("myId"));

If you can't add an id you could do something like this:

    WebElement result = driver.findElements(By.className("menu-popup-item-text")).stream()
                .filter(webElement -> webElement.getText().contains("Mr."))
                .findFirst().get();

Upvotes: 0

Related Questions