Back to Top

Get Selected Value From A Dropdown Field Using Casperjs

Updated 24 May 2017

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’s say you have a dropdown field with two options “Yes” and “NO” 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-

/**
* Webkul Software.
*
* @category Webkul
* @package Webkul_CasperJS
* @author Webkul
* @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https://webkul.com)
* @license https://store.webkul.com/license.html
*/

// Write your initial script to save the value in the field then write-

casper.then(function () {
	var f_selector = "#fblogin_enabled"; // Used id of the dropdown field you can use any proper selector of the field
	var f_selected = this.evaluate(function (f_selector) {
		var a = document.querySelector(f_selector);
		var b = a.children[a.selectedIndex];
		return {value: b.value,text: b.innerHTML};
	}, f_selector);
	this.echo("result: " + JSON.stringify(f_selected), 'INFO');   // Show the data in proper format
	facebook = f_selected.value;     //save the selected value in a variable
	console.log(facebook);
});

casper.run(function () {
	this.done();
});

After running the command you will get your desired output which you can view on terminal –

Start your headless eCommerce
now.
Find out More
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home