您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
Selenium處理select標簽的下拉框
作者:網絡轉載 發(fā)布時間:[ 2016/4/12 12:34:30 ] 推薦標簽:Selenium 功能測試

  有時候我們會碰到<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
  先以python為例,查看Selenium代碼select.py文件的實現(xiàn):
  ...seleniumwebdriversupportselect.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
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類的實現(xiàn)需要一個元素的定位。并且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)
  繼續(xù)查看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)
  繼續(xù)查看select_by_value() 方法符合我們的需求,它用于選取<option>標簽的value值。終,可以通過下面有實現(xiàn)選擇下拉框的選項。
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
  當然,在java中的用法也類似,不區(qū)別在語法層面有。
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"); //審核不通過
}
}

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd