java和C#有很多答案,但我找不到如何在javascript中做到这一点。似乎API是不同的.
发布于 2014-03-04 07:12:49
是的,这是可能的。假设我们有以下select元素:
<select name="test" id="select">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
您可以使用getValue获取当前选定的选项,也可以使用click更改选择。下面是一个简单的例子:
var webdriverjs = require('webdriverjs'),
client = webdriverjs.remote({desiredCapabilities:{browserName:'phantomjs'}}).init();
client
.url('http://localhost:8080')
.getValue('#select',function(err,val){
console.log(val); // will output "1"
})
.click('//*[@id="select"]/option[3]')
.getValue('#select',function(err,val){
console.log(val); // will output "3"
})
.end();
希望这能有所帮助。
干杯
发布于 2015-02-28 04:01:04
对于那些正在寻找纯WebDriverJS解决方案而不是特定于webdriver.io或protractor等框架的人,以下是代码,
var element = driver.findElement(wd.By.id('mySelect'));
element.getAttribute('value').then(function(selected) {
if(selected === 'the one I expected') {
done();
}
else {
done(new Error('something went wrong');
}
});
发布于 2016-04-13 23:45:37
如果您需要option元素,请使用then
回调的返回值:
driver.findElement(By.css('select[name=eventTypes]'))
.then((select) => {
return select.getAttribute('value').then((value) => {
return {select: select, value: value};
});
})
.then((data) => {
return data.select.findElement(By.css('option[value="'+data.value+'"]'));
})
.then((option) => {
//do something.
});
https://stackoverflow.com/questions/22153812
复制相似问题