ale*_*cxe 14 python testing selenium selenium-webdriver protractor
在Python,Java和其他几个selenium绑定中,对select->option
HTML结构(一个Select
类)有一个非常方便的抽象.
例如,假设有以下select
标记:
<select id="fruits" class="select" name="fruits">
<option value="1">Banana</option>
<option value="2">Mango</option>
</select>
Run Code Online (Sandbox Code Playgroud)
以下是我们如何在Python中操作它:
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_id('fruits'))
# get all options
print select.options
# get all selected options
print select.all_selected_options
# select an option by value
select.select_by_value('1')
# select by visible text
select.select_by_visible_text('Mango')
Run Code Online (Sandbox Code Playgroud)
换句话说,它是一个非常透明且易于使用的抽象.
是否有可能以类似的方式操纵量角器中的select
标签?
Del*_*kin 24
在Protractor中没有这样的东西,但我们可以写自己的:
选择-wrapper.js
'use strict';
var SelectWrapper = function(selector) {
this.webElement = element(selector);
};
SelectWrapper.prototype.getOptions = function() {
return this.webElement.all(by.tagName('option'));
};
SelectWrapper.prototype.getSelectedOptions = function() {
return this.webElement.all(by.css('option[selected="selected"]'));
};
SelectWrapper.prototype.selectByValue = function(value) {
return this.webElement.all(by.css('option[value="' + value + '"]')).click();
};
SelectWrapper.prototype.selectByPartialText = function(text) {
return this.webElement.all(by.cssContainingText('option', text)).click();
};
SelectWrapper.prototype.selectByText = function(text) {
return this.webElement.all(by.xpath('option[.="' + text + '"]')).click();
};
module.exports = SelectWrapper;
Run Code Online (Sandbox Code Playgroud)
用法
var SelectWrapper = require('select-wrapper');
var mySelect = new SelectWrapper(by.id('fruits'));
# select an option by value
mySelect.selectByValue('1');
# select by visible text
mySelect.selectByText('Mango');
Run Code Online (Sandbox Code Playgroud)
请注意,Select是JavaScript中的保留字
归档时间: |
|
查看次数: |
3966 次 |
最近记录: |