TestProject Forum
Powered by leading experts in the test automation community

Selenium WebElement.getText() on bootstrap dropdown items not working

I’m trying to get the text for each list item in bootstrap buttonDropdown.

When I invoke getText() on the dropdown element’s list items the string I get is empty, even though I know I have the right anchor tag elements, since when I run JavaScriptExecutor the correct strings are returned.

List<WebElement> dropdownItemsAnchors = buttonDropdownContainer.findElements(By.xpath("./ul//li//a"));
System.out.println(dropdownItemsAnchors.get(0).getText());

prints empty string.

JavascriptExecutor jsDriver = (JavascriptExecutor)getDriver(); String item = (String)jsDriver.executeScript("return arguments[0].innerHTML", dropdownItemsAnchors.get(0)); System.out.println(item);
prints correct string however.

Try clicking on the button dropdown first, then search for child elements.

should work.

They probably don’t exist for selenium before you click on their parent