如何使用node.js在selenium webdriver中选择下拉值
how to select a dropdown value in selenium webdriver using node.js
我有以下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');
相关文章:
- 下拉选择可自动更改第二个下拉选择
- 如何使用jQuery选择下拉列表的值
- jquery点击函数select&取消选择
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 按照选项卡索引的顺序循环一个jQuery选择
- 在动态创建的元素上获取对特定选择器的引用
- AngularJS-在JSON选择器中使用变量名
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- 获取选择框的状态
- d3基于用户选择动态更新节点
- 提交后保留下拉选择的值
- JQuery对动态创建的对象进行选择
- 无法在Ionic select中预先选择最后一个选项
- Webdriver.io pageObject模式-通过传递参数来定义元素选择器
- 如何使用Selenium Webdriver和Java从href html标签中选择“Page=?” valuse
- 如何使用node.js在selenium webdriver中选择下拉值
- 如何在Selenium WebDriver中为出生日期选择日期选择器
- 如何用javascript选择Selenium-WebDriver的所有复选框
- Selenium WebDriver w/Java在弹出窗口中选择项目时出现问题
- 无法使用webdriver选择下拉菜单