Lately I was writing automation tests for a web applications that was using Dojo Toolkit. One of the problems that I’ve encountered was that I was not able to select an item from what seemed like a regular dropdown menu. With normal dropdowns that have “select” tag in html Selenium has a simple way to grab them
Select dropdown = new Select(driver.findElement(By.id("identifier")));
and you can select the values either by
dropdown.selectByVisibleText("TEST ");
or by index
dropdown.selectByIndex(1);
But when I was digging deeper it seemed like the dojo element handled “dropdowns” in a different way. They are tables with only one row but many columns. The problem comes when you try to select the values in them.
When you go through the elements in the table one by one and print out the text of each item in the table the getText() method prints either “null” or an empty string. The solution to this problem is that the elements found in that table are found under dijitMenuItemLabel class and in my case had an id attirbute with
In my case I wanted my code to be flexible and select always the last item in the dropdown menu. The idea behind the next code snippet is that it cycles through an array of all the elements that i found previous, matches them by the id of the element and does something based on which element it matches.
In this case if it finds an element that has an attribute id that contains “Select” it goes in to this:
// find the dropdown button e being an element that we currently going through
WebElement dropDownButton = driver.findElement(By.xpath("//table[@id='" + e.getAttribute("id") + "']/tbody/tr/td"));
// click on it
dropDownButton.click();
// find all of the elements inside the dropdown menu,
List<WebElement> test = driver.findElements(By.xpath("//td[contains(@id, 'text')]"));
// find the last element in the dropdown list and click on it,
test.get(test.size() - 1).click();