Selenium處置select標簽的下拉框。本站提示廣大學習愛好者:(Selenium處置select標簽的下拉框)文章只能為提供參考,不一定能成為您想要的結果。以下是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"); //審核欠亨過
}
}