{"id":84480,"date":"2017-05-23T07:34:44","date_gmt":"2017-05-23T07:34:44","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=84480"},"modified":"2017-05-24T07:28:49","modified_gmt":"2017-05-24T07:28:49","slug":"get-selected-value-dropdown-field-using-casper","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/","title":{"rendered":"Get Selected Value From A Dropdown Field Using Casperjs"},"content":{"rendered":"<p>As casper does not provide any function to get the value of a field directly, you can perform following DOM operations to get the value and innerHTML.<\/p>\n<p>Let&#8217;s say you have a dropdown field with two options &#8220;Yes&#8221; and &#8220;NO&#8221; and you want to get the value which you have saved in the field. After writing initial scripts for selecting and saving the value in the fields, you can perform the following tasks-<\/p>\n<pre class=\"brush:js\">\/**\r\n* Webkul Software.\r\n*\r\n* @category Webkul\r\n* @package Webkul_CasperJS\r\n* @author Webkul\r\n* @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https:\/\/webkul.com)\r\n* @license https:\/\/store.webkul.com\/license.html\r\n*\/\r\n\r\n\/\/ Write your initial script to save the value in the field then write-\r\n\r\ncasper.then(function () {\r\n\tvar f_selector = \"#fblogin_enabled\"; \/\/ Used id of the dropdown field you can use any proper selector of the field\r\n\tvar f_selected = this.evaluate(function (f_selector) {\r\n\t\tvar a = document.querySelector(f_selector);\r\n\t\tvar b = a.children[a.selectedIndex];\r\n\t\treturn {value: b.value,text: b.innerHTML};\r\n\t}, f_selector);\r\n\tthis.echo(\"result: \" + JSON.stringify(f_selected), 'INFO');   \/\/ Show the data in proper format\r\n\tfacebook = f_selected.value;     \/\/save the selected value in a variable\r\n\tconsole.log(facebook);\r\n});\r\n\r\ncasper.run(function () {\r\n\tthis.done();\r\n});<\/pre>\n<p>After running the command you will get your desired output which you can view on terminal &#8211;<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-84484\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/drop1.png\" alt=\"\" width=\"616\" height=\"139\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/drop1.png 616w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/drop1-250x56.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/drop1-300x68.png 300w\" sizes=\"(max-width: 616px) 100vw, 616px\" loading=\"lazy\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As casper does not provide any function to get the value of a field directly, you can perform following DOM operations to get the value and innerHTML. Let&#8217;s say you have a dropdown field with two options &#8220;Yes&#8221; and &#8220;NO&#8221; and you want to get the value which you have saved in the field. After <a href=\"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":66,"featured_media":84482,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4313],"tags":[3505,1908,4861,4859,4860],"class_list":["post-84480","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-casperjs","tag-casperjs","tag-dropdown","tag-innerhtml","tag-selected","tag-value"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Get Selected Value From A Dropdown Field Using Casperjs - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Get Selected Value From A Dropdown Field Using Casperjs - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"As casper does not provide any function to get the value of a field directly, you can perform following DOM operations to get the value and innerHTML. Let&#8217;s say you have a dropdown field with two options &#8220;Yes&#8221; and &#8220;NO&#8221; and you want to get the value which you have saved in the field. After [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-05-23T07:34:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-05-24T07:28:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\" \/>\n\t<meta property=\"og:image:width\" content=\"825\" \/>\n\t<meta property=\"og:image:height\" content=\"260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Shruti Baranwal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Shruti Baranwal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/\"},\"author\":{\"name\":\"Shruti Baranwal\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/d90db6dde51254f8d7bda2539b81386e\"},\"headline\":\"Get Selected Value From A Dropdown Field Using Casperjs\",\"datePublished\":\"2017-05-23T07:34:44+00:00\",\"dateModified\":\"2017-05-24T07:28:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/\"},\"wordCount\":98,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\",\"keywords\":[\"casperjs\",\"dropdown\",\"innerHTML\",\"selected\",\"value\"],\"articleSection\":[\"CasperJS\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/\",\"url\":\"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/\",\"name\":\"Get Selected Value From A Dropdown Field Using Casperjs - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\",\"datePublished\":\"2017-05-23T07:34:44+00:00\",\"dateModified\":\"2017-05-24T07:28:49+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\",\"width\":\"825\",\"height\":\"260\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Get Selected Value From A Dropdown Field Using Casperjs\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/d90db6dde51254f8d7bda2539b81386e\",\"name\":\"Shruti Baranwal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2ecd6b094113b3e2f346229e04e90fcb6e242f28b3a1dbcb2f44fd9eefb93dbc?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2ecd6b094113b3e2f346229e04e90fcb6e242f28b3a1dbcb2f44fd9eefb93dbc?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"caption\":\"Shruti Baranwal\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/shruti-baranwal890\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Get Selected Value From A Dropdown Field Using Casperjs - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/","og_locale":"en_US","og_type":"article","og_title":"Get Selected Value From A Dropdown Field Using Casperjs - Webkul Blog","og_description":"As casper does not provide any function to get the value of a field directly, you can perform following DOM operations to get the value and innerHTML. Let&#8217;s say you have a dropdown field with two options &#8220;Yes&#8221; and &#8220;NO&#8221; and you want to get the value which you have saved in the field. After [...]","og_url":"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2017-05-23T07:34:44+00:00","article_modified_time":"2017-05-24T07:28:49+00:00","og_image":[{"width":825,"height":260,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","type":"image\/png"}],"author":"Shruti Baranwal","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Shruti Baranwal","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/"},"author":{"name":"Shruti Baranwal","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/d90db6dde51254f8d7bda2539b81386e"},"headline":"Get Selected Value From A Dropdown Field Using Casperjs","datePublished":"2017-05-23T07:34:44+00:00","dateModified":"2017-05-24T07:28:49+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/"},"wordCount":98,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","keywords":["casperjs","dropdown","innerHTML","selected","value"],"articleSection":["CasperJS"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/","url":"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/","name":"Get Selected Value From A Dropdown Field Using Casperjs - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","datePublished":"2017-05-23T07:34:44+00:00","dateModified":"2017-05-24T07:28:49+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","width":"825","height":"260"},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/get-selected-value-dropdown-field-using-casper\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Get Selected Value From A Dropdown Field Using Casperjs"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/d90db6dde51254f8d7bda2539b81386e","name":"Shruti Baranwal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2ecd6b094113b3e2f346229e04e90fcb6e242f28b3a1dbcb2f44fd9eefb93dbc?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2ecd6b094113b3e2f346229e04e90fcb6e242f28b3a1dbcb2f44fd9eefb93dbc?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","caption":"Shruti Baranwal"},"url":"https:\/\/webkul.com\/blog\/author\/shruti-baranwal890\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/84480","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/66"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=84480"}],"version-history":[{"count":6,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/84480\/revisions"}],"predecessor-version":[{"id":84491,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/84480\/revisions\/84491"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media\/84482"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=84480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=84480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=84480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}