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

how to select a dropdown value in selenium webdriver using node.js

本文关键字:选择 webdriver selenium 何使用 node js      更新时间:2023-09-26

我有以下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>

那么我该如何从列表中获取该项目呢?

我是Node.Js(JavaScript)的新手,所以我不知道如何在node.Js中实现它,但它可以在java中实现,如下所示:

Select dropdown = new Select(driver.findElement(By.class("dropdown-menu inner selectpicker"))); 
dropdown.selectByVisibleText("Andorra");

只需单击所需选项。

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

或按价值

driver.findElement(webdriver.By.css('#mySelection > option[value=apple]'))
    .click();

请注意,我已将您的"By"更改为css选择器。我很懒,喜欢从开发人员工具中钻取该选项,然后选择Copy CSS Path(chrome)或Copy Unique Selector(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();
            });
        });
    });
}

对于任何对此感兴趣的人来说,以下是我为<select>选项所做的操作:

<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 for NodeJS中,您可以获取It <option>:

var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
   withCapabilities(webdriver.Capabilities.chrome()).
   build();

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

当您为该选项.sendKeys()时,它必须等于下拉列表中显示给用户的文本。

在您的情况下,只需获取父元素,然后为子元素获取sendKeys()

我会使用Cheerio。

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='cat'>Cat!</option>
   <option value='dog'>Dog!</option>
   <option value='rat'>Rat!</option>
</select>

您可以使用方法sendKeys传递值或文本

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

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