{"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\"> &lt; 1<\/span> <span class=\"rt-label rt-postfix\">minute read<\/span><\/span><p class=\"lead\">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           \r\n            <div class=\"onp-locker-call\"  data-lock-id=\"onpLock937302\">\r\n                <p>\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.\n<\/p>\r\n            <\/div>\r\n         \r\n\r\n        <\/p>\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\"> &lt; 1<\/span> <span class=\"rt-label rt-postfix\">minute read<\/span><\/span>During UI test automation you will need to write tests that validate the options that are present in a select tag. Getting all the options in the select tag is very simple using Selenium Webdriver and below is how it is done. 1. In the page object model of the page you create a WebElement&#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":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[9,8,7,6],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/61"}],"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: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}]}}