Clicking a dropdown

//Example: Selecting an option and retrieving the selected label/value
var By = $driver.By,
selectbox;
// ----------------------------------------------------------------------------- //
// Helper function that returns the first selected option given a select element
function findSelected(select) {
var d = $driver.promise.defer();
select.findElements(By.tagName('option')).then(function (options) {
options.forEach(function(option) {
option.isSelected().then(function(selected) {
if (selected) {
d.fulfill(option);
}
});
});
});
return d.promise;
}
// ----------------------------------------------------------------------------- //
// Load the page
$browser.get("http://www.w3schools.com/html/tryit.asp?filename=tryhtml_select2").then(function(){
  // Focus the iframe
  $browser.switchTo().frame("iframeResult");
  // Click on select
    return $browser.waitForAndFindElement(By.name("cars"), 15000).then(function(elem) {
    selectbox = elem;
    selectbox.click();
  });
}).then(function() {
  // Select an option
    return selectbox.findElement(By.css("option[value='audi']")).click();
})
1 Like

Thanks! this worked for me with a small change: Wait till dropdown opens and elements are seen.

var assert = require(‘assert’);
let selectbox;
let row;

$browser.get(“https://courses.edx.org/register?next=/dashboard”).then(function(){
// Find country dropdown
return $browser.waitForAndFindElement($driver.By.name(“country”), 15000)
.then(function(elem) {
selectbox = elem;
selectbox.click()

//Wait till dropdown opens
.then(function() {
return $browser.waitForAndFindElement($driver.By.css(“option[value=‘AM’]”), 15000)
.then(function(countryname){
row = countryname;
row.click();
});
});

});

});

3 Likes