在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Selenium处理select标签的下拉框

2016-7-29 15:41| 发布者: zhangjf| 查看: 585| 评论: 0

摘要: Selenium是一个开源的和便携式的自动化软件测试工具,用于测试Web应用程序有能力在不同的浏览器和操作系统运行。Selenium真的不是一个单一的工具,而是一套工具,帮助测试者更有效地基于Web的应用程序的自动化。 有 ...

Selenium是一个开源的和便携式的自动化软件测试工具,用于测试Web应用程序有能力在不同的浏览器和操作系统运行。Selenium真的不是一个单一的工具,而是一套工具,帮助测试者更有效地基于Web的应用程序的自动化。

有时候我们会碰到标签的下拉框。直接点击下拉框中的选项不一定可行。Selenium专门提供了Select类来处理下拉框。

  1. <select id="status" class="form-control valid" onchange="" name="status">
  2. <option value=""></option>
  3. <option value="0">未审核</option>
  4. <option value="1">初审通过</option>
  5. <option value="2">复审通过</option>
  6. <option value="3">审核不通过</option>
  7. </select>
复制代码

Python-selenium中的操作  

 先以python为例,查看Selenium代码select.py文件的实现:

  ...seleniumwebdriversupportselect.py

class Select:

  1. def __init__(self, webelement):
  2. """
  3. Constructor. A check is made that the given element is, indeed, a SELECT tag. If it is not,
  4. then an UnexpectedTagNameException is thrown.
  5. :Args:
  6. - webelement - element SELECT element to wrap
  7. Example:
  8. from selenium.webdriver.support.ui import Select n
  9. Select(driver.find_element_by_tag_name("select")).select_by_index(2)
  10. """
  11. if webelement.tag_name.lower() != "select":
  12. raise UnexpectedTagNameException(
  13. "Select only works on <select> elements, not on <%s>" %
  14. webelement.tag_name)
  15. self._el = webelement
  16. multi = self._el.get_attribute("multiple")
  17. self.is_multiple = multi and multi != "false"
复制代码

  查看Select类的实现需要一个元素的定位。并且Example中给了例句。

  1.   Select(driver.find_element_by_tag_name("select")).select_by_index(2)
  2. def select_by_index(self, index):
  3. """Select the option at the given index. This is done by examing the "index" attribute of an
  4. element, and not merely by counting.
  5. :Args:
  6. - index - The option at this index will be selected
  7. """
  8. match = str(index)
  9. matched = False
  10. for opt in self.options:
  11. if opt.get_attribute("index") == match:
  12. self._setSelected(opt)
  13. if not self.is_multiple:
  14. return
  15. matched = True
  16. if not matched:
  17. raise NoSuchElementException("Could not locate element with index %d" % index)
复制代码

  继续查看select_by_index() 方法的使用并符合上面的给出的下拉框的要求,因为它要求下拉框的选项必须要有index属性,例如index=”1”。

  1. def select_by_value(self, value):
  2. """Select all options that have a value matching the argument. That is, when given "foo" this
  3. would select an option like:
  4. <option value="foo">Bar</option>
  5. :Args:
  6. - value - The value to match against
  7. """
  8. css = "option[value =%s]" % self._escapeString(value)
  9. opts = self._el.find_elements(By.CSS_SELECTOR, css)
  10. matched = False
  11. for opt in opts:
  12. self._setSelected(opt)
  13. if not self.is_multiple:
  14. return
  15. matched = True
  16. if not matched:
  17. raise NoSuchElementException("Cannot locate option with value: %s" % value)
复制代码

  继续查看select_by_value() 方法符合我们的要求,它用于选取

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"); //审核不通过
}
}

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-6 12:56

Copyright 2015-2025 djqfx

返回顶部