{"id":61,"date":"2015-07-21T11:16:16","date_gmt":"2015-07-21T11:16:16","guid":{"rendered":"http:\/\/loadfocus.com\/blog\/tech\/?p=61"},"modified":"2019-04-26T08:42:49","modified_gmt":"2019-04-26T08:42:49","slug":"how-to-get-all-the-options-in-a-select-tag-using-selenium-webdriver","status":"publish","type":"post","link":"https:\/\/loadfocus.com\/blog\/tech\/2015\/07\/how-to-get-all-the-options-in-a-select-tag-using-selenium-webdriver","title":{"rendered":"How to get all the options in a select tag using Selenium Webdriver"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 2<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span><p class=\"lead\"><!-- pn-tldr --><\/p>\n<h2>Key takeaways<\/h2>\n<ul>\n<li>The Select class exposes getOptions() for real select elements.<\/li>\n<li>Custom dropdowns built from divs need normal element queries instead.<\/li>\n<li>Assert on the option text or value, not the count alone, which passes on the wrong list.<\/li>\n<\/ul>\n<p><!-- \/pn-tldr -->During UI test automation you will need to write tests that validate the options that are present in a select tag.<br \/>\nGetting all the options in the select tag is very simple using Selenium Webdriver and below is how it is done.<\/p>\n<p>1. In the page object model of the page you create a WebElement that identifies the select tag; example class presented below:<\/p>\n<pre class=\"lang-java\"><code class=\"lang-java\">\nimport org.openqa.selenium.WebElement;\nimport org.openqa.selenium.support.FindBy;\n\n\npublic class PageObject {\n\npublic String[] occupantNoOptionExpectedArray = {\"1\", \"2\", \"3\", \"4\", \"5+\"};\n\n@FindBy(xpath = \"\/\/select[@id='numberofoccupants']\")\npublic WebElement noOfOccupantsDropdown;\n\n}\n<\/code><\/pre>\n<p>2. In the test you will create a Select object (class org.openqa.selenium.support.ui.Select) like in the below example:<\/p>\n<pre class=\"lang-java\"><code class=\"lang-java\">\nimport java.util.List;\n\n\nimport org.openqa.selenium.WebElement;\nimport org.openqa.selenium.support.ui.Select;\nimport org.testng.Assert;\nimport org.testng.annotations.Test;\n\npublic class OccupantPageTest {\n\nPageObject pageObject = new PageObject();\n[emaillocker]\n@Test\npublic void test_Occupants_Drop_Down_Values() throws Exception{\nSelect selectBedroom = new Select(pageObject.noOfOccupantsDropdown);\nList&lt;WebElement&gt; optionSelect = selectBedroom.getOptions();\n\nfor (int i = 0; i &lt; optionSelect.size(); i++){\nAssert.assertEquals(optionSelect.get(i).getText(), pageObject.occupantNoOptionExpectedArray[i], \"Values in the drop down for number of occupants are wrong\"  );\n}\n}\n}\n<\/code><\/pre>\n<p>That&#8217;s it. Simple. Enjoy.<br \/>\n[\/emaillocker]<!-- pn-faq --><\/p>\n<h2>Frequently Asked Questions<\/h2>\n<h3>How do I read every option from a dropdown?<\/h3>\n<p>Wrap the element in Selenium&#8217;s Select class and call getOptions(), which returns the option elements as a list. Read getText() from each one to compare against your expected values.<\/p>\n<h3>Does this work for custom dropdowns?<\/h3>\n<p>No. The Select class only works on a real HTML select element. A dropdown built from divs and list items is just ordinary markup, so you locate and read those elements the normal way.<\/p>\n<h3>What should the assertion check?<\/h3>\n<p>The text or the values, not only the count. A test that asserts five options pass happily when the list is five wrong options, which is exactly the regression you were trying to catch.<\/p>\n<p><!-- \/pn-faq --><!-- pn-related-reading --><\/p>\n<h2>Related reading<\/h2>\n<ul>\n<li><a href=\"https:\/\/loadfocus.com\/blog\/2014\/06\/how-to-take-snapshots-of-the-browser-using-selenium-webdriver\">How to take screenshots with Selenium WebDriver<\/a><\/li>\n<li><a href=\"https:\/\/loadfocus.com\/blog\/tech\/2015\/06\/how-to-clear-local-storage-using-selenium-webdriver\">How to Clear Local Storage using Selenium WebDriver?<\/a><\/li>\n<li><a href=\"https:\/\/loadfocus.com\/blog\/tech\/2015\/07\/how-to-find-the-xpath-of-your-selenium-webdriver-test-using-chrome-browser\">Find the XPath for a Selenium Test Using Chrome<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 2<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span>Key takeaways The Select class exposes getOptions() for real select elements. Custom dropdowns built from divs need normal element queries instead. Assert on the option text or value, not the count alone, which passes on the wrong list. During UI test automation you will need to write tests that validate the options that are present&#8230;  <a href=\"https:\/\/loadfocus.com\/blog\/tech\/2015\/07\/how-to-get-all-the-options-in-a-select-tag-using-selenium-webdriver\" class=\"more-link\" title=\"Read How to get all the options in a select tag using Selenium Webdriver\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":500,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[9,8,7,6],"class_list":["post-61","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ui-testing","tag-dropdown","tag-select","tag-selenium","tag-webdriver"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/61","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/comments?post=61"}],"version-history":[{"count":7,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/61\/revisions"}],"predecessor-version":[{"id":323,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/61\/revisions\/323"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/media\/500"}],"wp:attachment":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/media?parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/categories?post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/tags?post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}