Selenium处理select标签的下拉框
selenium是一个开源的和便携式的自动化软件测试工具,用于测试web应用程序有能力在不同的浏览器和操作系统运行。selenium真的不是一个单一的工具,而是一套工具,帮助测试者更有效地基于web的应用程序的自动化。
有时候我们会碰到<select></select>标签的下拉框。直接点击下拉框中的选项不一定可行。selenium专门提供了select类来处理下拉框。
<select id="status" class="form-control valid" onchange="" name="status"> <option value=""></option> <option value="0">未审核</option> <option value="1">初审通过</option> <option value="2">复审通过</option> <option value="3">审核不通过</option> </select>
python-selenium中的操作
先以python为例,查看selenium代码select.py文件的实现:
...\selenium\webdriver\support\select.py
class select:
def __init__(self, webelement): """ constructor. a check is made that the given element is, indeed, a select tag. if it is not, then an unexpectedtagnameexception is thrown. :args: - webelement - element select element to wrap example: from selenium.webdriver.support.ui import select \n select(driver.find_element_by_tag_name("select")).select_by_index(2) """ if webelement.tag_name.lower() != "select": raise unexpectedtagnameexception( "select only works on <select> elements, not on <%s>" % webelement.tag_name) self._el = webelement multi = self._el.get_attribute("multiple") self.is_multiple = multi and multi != "false"
查看select类的实现需要一个元素的定位。并且example中给了例句。
select(driver.find_element_by_tag_name("select")).select_by_index(2) def select_by_index(self, index): """select the option at the given index. this is done by examing the "index" attribute of an element, and not merely by counting. :args: - index - the option at this index will be selected """ match = str(index) matched = false for opt in self.options: if opt.get_attribute("index") == match: self._setselected(opt) if not self.is_multiple: return matched = true if not matched: raise nosuchelementexception("could not locate element with index %d" % index)
继续查看select_by_index() 方法的使用并符合上面的给出的下拉框的要求,因为它要求下拉框的选项必须要有index属性,例如index=”1”。
def select_by_value(self, value): """select all options that have a value matching the argument. that is, when given "foo" this would select an option like: <option value="foo">bar</option> :args: - value - the value to match against """ css = "option[value =%s]" % self._escapestring(value) opts = self._el.find_elements(by.css_selector, css) matched = false for opt in opts: self._setselected(opt) if not self.is_multiple: return matched = true if not matched: raise nosuchelementexception("cannot locate option with value: %s" % value)
继续查看select_by_value() 方法符合我们的要求,它用于选取<option>标签的value值。最终,可以通过下面有实现选择下拉框的选项。
from selenium.webdriver.support.select import select
……
sel = driver.find_element_by_xpath("//select[@id='status']")
select(sel).select_by_value('0') #未审核
select(sel).select_by_value('1') #初审通过
select(sel).select_by_value('2') #复审通过
select(sel).select_by_value('3') #审核不通过
java-selenium中的操作
当然,在java中的用法也类似,唯一不区别在语法层面有。
package com.jase.base;
import org.openqa.selenium.webdriver;
import org.openqa.selenium.by.byid;
import org.openqa.selenium.chrome.chromedriver;
import org.openqa.selenium.support.ui.select;
public class selecttest {
public static void main(string[] args){
webdriver driver = new chromedriver();
driver.get("http://www.you_url.com");
// ……
select sel = new select(driver.findelement(byid.xpath("//select[@id='status']")));
sel.selectbyvalue("0"); //未审核
sel.selectbyvalue("1"); //初审通过
sel.selectbyvalue("2"); //复审通过
sel.selectbyvalue("3"); //审核不通过
}
}
下一篇: asp.net url 伪静态设置方法
推荐阅读