Javascript 如何使用node.js在selenium webdriver中选择下拉值

Javascript 如何使用node.js在selenium webdriver中选择下拉值,javascript,selenium,Javascript,Selenium,我有以下HTML结构: <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: h

我有以下HTML结构:

<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>
我会用这个

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();
    }
};

或者简单地使用
.execute()
命令

对于任何陷入此问题的人,下面是我如何为
选项执行此操作的:

<select id='mySelection'>
   <option value='0'>Hello</option>
   <option value='1'>Everybody</option>
   <option value='2'>Got</option>
   <option value='3'>It</option>
</select>
当您为选项选择
.sendKeys()
时,它必须等于下拉列表中显示给用户的文本


在您的情况下,只需抓取父元素,然后单击所需选项即可获取子元素的sendKeys()。

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

请注意,我已将您的“By”更改为css选择器。我很懒,喜欢钻研开发者工具中的选项,然后选择复制CSS路径(chrome)或复制唯一选择器(firefox)。

您可以创建一个函数来选择所需的值

像这样

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();
            });
        });
    });
}

Node实际上没有任何解析HTML的方法,但是您可以看看类似的东西,并使用非常类似于jQuery语法的东西来获得您想要的。脱离轨道!!回答作为“浏览器”传递给函数的是什么?是的,这对我来说不起作用,sendKeys()基本上是发送值,但父元素不是字段或实际接受值的东西。
driver.findElement(webdriver.By.css('#mySelection > option:nth-child(4)'))
    .click();
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();
            });
        });
    });
}