How to select dropdown value in selenium webdriver using node.js

I have the following HTML structure:

<button class="dropdown-toggle selectpicker btn btn-primary" data-toggle="dropdown" type="button" data-id="strategic_reseller_country" title="United States">
<div class="dropdown-menu open" style="max-height: 245.6px; overflow: hidden; min-height: 40px;">
<ul class="dropdown-menu inner selectpicker" role="menu" style="max-height: 243.6px; overflow-y: auto; min-height: 38px;">
<li class="" rel="0">
<a class="" style="" tabindex="0">
<span class="text"/>
<i class="glyphicon glyphicon-ok icon-ok check-mark"/>
</a>
</li>
<li rel="1">
<a class="" style="" tabindex="0">
<span class="text">Andorra</span>
<i class="glyphicon glyphicon-ok icon-ok check-mark"/>
</a>
</li>
<li rel="2">
<a class="" style="" tabindex="0">
<span class="text">United Arab Emirates</span>
<i class="glyphicon glyphicon-ok icon-ok check-mark"/>
</a>
</li>
<li rel="3">
<a class="" style="" tabindex="0">
<span class="text">Afghanistan</span>
<i class="glyphicon glyphicon-ok icon-ok check-mark"/>
</a>
</li>
<li rel="4">
<li rel="5">
<li rel="6">
<li rel="7">
<li rel="8">
<li rel="9">
<a class="" style="" tabindex="0">
<span class="text">Netherlands Antilles</span>
<i class="glyphicon glyphicon-ok icon-ok check-mark"/>
</a>
</li> 
</ul>
</div>

So how can I get an item from a list?

I am new to Node.Js(JavaScript), so I don’t know how to achieve this in node.Js, but this can be achieved in Java as follows:

Select dropdown = new Select(driver.findElement(By.class("dropdown-menu inner selectpicker"))); 
dropdown.selectByVisibleText("Andorra");
+8
source share
4 answers

I would use Cheerio for this.

module.exports = {
    "login": function (browser) {
        var cheerio = require("cheerio")

        browser
            .url("http://www.wikipedia.org")
            .waitForElementVisible('body', 1000)
            .waitForElementVisible('select#searchLanguage', 1000)
            .source(function(result) { // .source() dump the page source in the variable
                $ = cheerio.load(result.value)
                var num_languages = $('select#searchLanguage option').length
                console.log('Wikipedia.org Languages: ' + num_languages);
            })
            .end();
    }
};

or just using the command .execute()

+2
source

Just click the option you want.

driver.findElement(webdriver.By.css('#mySelection > option:nth-child(4)'))
    .click();

, "By" css. CSS- () (firefox).

+11

,

...

driver.wait(
    until.elementLocated(By.id("continents")), 20000
).then(element => {
    selectByVisibleText(element, "Africa")
});

function selectByVisibleText(select, textDesired) {
    select.findElements(By.tagName('option'))
    .then(options => {
        options.map(option => {
            option.getText().then(text => {
                if (text == textDesired)
                    option.click();
            });
        });
    });
}
+5

, , , :

<select id='mySelection'>
   <option value='0'>Hello</option>
   <option value='1'>Everybody</option>
   <option value='2'>Got</option>
   <option value='3'>It</option>
</select>

Selenium NodeJS "":

var webdriver = require('selenium-webdriver');

var driver = new webdriver.Builder().
   withCapabilities(webdriver.Capabilities.chrome()).
   build();


driver.findElement(webdriver.By.id('mySelection')).sendKeys('3');

.sendKeys() , = ''

, sendKeys() .

0

All Articles