Chris Byatt
Chris Byatt

Reputation: 3819

Selenium - search for items inside parent

I've just started writing some selenium autotests in java for my HTML5 web application. I was wondering if it was possible rather than adding id attributes onto everything to search for a number of items inside a div with an id.

For example, I have a div with id: sfn-PopupMenu. There are several items with class="gwt-MenuItem" and role="menuitem". And I'd like to be able to find and control each of these without giving them individual Ids.

Is this possible? Or should I just put ids on everything.

Thanks,

Chris

Upvotes: 0

Views: 94

Answers (1)

LaurentG
LaurentG

Reputation: 11757

With Selenium, you can also search elements by CSS selectors or XPath expressions. Without the actual HTML code it is hard to say what is the best here. But in general, you should first try to use ID, then CSS and if not possible XPath.

You could perhaps do something like this:

driver.findElements(By.cssSelector("#sfn-PopupMenu .gwt-MenuItem"));

Upvotes: 1

Related Questions