国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Selenium處理select標簽的下拉框

2019-11-26 14:27:04
字體:
來源:轉載
供稿:網友

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 wrapExample:from selenium.webdriver.support.ui import Select /nSelect(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 = webelementmulti = 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 anelement, and not merely by counting.:Args:- index - The option at this index will be selected """match = str(index)matched = Falsefor opt in self.options:if opt.get_attribute("index") == match:self._setSelected(opt)if not self.is_multiple:returnmatched = Trueif 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" thiswould 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 = Falsefor opt in opts:self._setSelected(opt)if not self.is_multiple:returnmatched = Trueif 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("http://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("http://select[@id='status']")));
sel.selectByValue("0"); //未審核
sel.selectByValue("1"); //初審通過
sel.selectByValue("2"); //復審通過
sel.selectByValue("3"); //審核不通過
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汝州市| 图木舒克市| 墨竹工卡县| 石嘴山市| 南岸区| 葵青区| 辽宁省| 申扎县| 富锦市| 孙吴县| 吴桥县| 黄大仙区| 广汉市| 万山特区| 紫金县| 临邑县| 金湖县| 陵川县| 广元市| 北京市| 沙洋县| 临西县| 吉水县| 宁强县| 黄石市| 连山| 沭阳县| 永安市| 永福县| 温泉县| 盘山县| 屏南县| 隆昌县| 连平县| 张家界市| 安岳县| 若尔盖县| 东安县| 涡阳县| 藁城市| 汕尾市|