`
duoluohua
  • 浏览: 12939 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

selenium中AddSelection的用法

 
阅读更多

此方法一般很少用。看了selenium的源码。下面首先看一下selenium的doSelect方法。也就是我们在选择下拉菜单用的select方法。可以通过index,label,value,id四种方式来select下拉菜单中的option。
doSelect源码如下:

Selenium.prototype.doSelect = function(selectLocator, optionLocator) {
/**
* Select an option from a drop-down using an option locator.
*
* <p>
* Option locators provide different ways of specifying options of an HTML
* Select element (e.g. for selecting a specific option, or for asserting
* that the selected option satisfies a specification). There are several
* forms of Select Option Locator.
* </p>
* <ul>
* <li><strong>label</strong>=<em>labelPattern</em>:
* matches options based on their labels, i.e. the visible text. (This
* is the default.)
* <ul class="first last simple">
* <li>label=regexp:^[Oo]ther</li>
* </ul>
* </li>
* <li><strong>value</strong>=<em>valuePattern</em>:
* matches options based on their values.
* <ul class="first last simple">
* <li>value=other</li>
* </ul>
*
*
* </li>
* <li><strong>id</strong>=<em>id</em>:
*
* matches options based on their ids.
* <ul class="first last simple">
* <li>id=option1</li>
* </ul>
* </li>
* <li><strong>index</strong>=<em>index</em>:
* matches an option based on its index (offset from zero).
* <ul class="first last simple">
*
* <li>index=2</li>
* </ul>
* </li>
* </ul>
* <p>
* If no option locator prefix is provided, the default behaviour is to match on <strong>label</strong>.
* </p>
*
*
* @param selectLocator an <a href="#locators">element locator</a> identifying a drop-down menu
* @param optionLocator an option locator (a label by default)
*/
var element = this.browserbot.findElement(selectLocator);
if (!("options" in element)) {
throw new SeleniumError("Specified element is not a Select (has no options)");
}
var locator = this.optionLocatorFactory.fromLocatorString(optionLocator);
var option = locator.findOption(element);
this.browserbot.selectOption(element, option);
};

doAddSelection的源码如下
Selenium.prototype.doAddSelection = function(locator, optionLocator) {
/**
* Add a selection to the set of selected options in a multi-select element using an option locator.
*
* @see #doSelect for details of option locators
*
* @param locator an <a href="#locators">element locator</a> identifying a multi-select box
* @param optionLocator an option locator (a label by default)
*/
var element = this.browserbot.findElement(locator);
if (!("options" in element)) {
throw new SeleniumError("Specified element is not a Select (has no options)");
}
var locator = this.optionLocatorFactory.fromLocatorString(optionLocator);
var option = locator.findOption(element);
this.browserbot.addSelection(element, option);
};

如果下拉菜单中没有以上标签除index之外,可以通过addSelection这个方法来添加选项。

例如:
<ul class="menu">
<li>test="a"</li>
<li>test="b"</li>
</ul>
我们就可以用selenium.addSelection("//ul[@class='menu']","test")
selenium.select("test=xxx");
以上纯属于个人研究,如果有什么不对的,请指教,共同学习。谢谢。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics