Selenium處理select標(biāo)簽的下拉框
Selenium是一個(gè)開(kāi)源的和便攜式的自動(dòng)化軟件測(cè)試工具,用于測(cè)試Web應(yīng)用程序有能力在不同的瀏覽器和操作系統(tǒng)運(yùn)行。Selenium真的不是一個(gè)單一的工具,而是一套工具,幫助測(cè)試者更有效地基于Web的應(yīng)用程序的自動(dòng)化。
有時(shí)候我們會(huì)碰到<select></select>標(biāo)簽的下拉框。直接點(diǎn)擊下拉框中的選項(xiàng)不一定可行。Selenium專(zhuān)門(mén)提供了Select類(lèi)來(lái)處理下拉框。

<select id="status" class="form-control valid" onchange="" name="status"> <option value=""></option> <option value="0">未審核</option> <option value="1">初審?fù)ㄟ^(guò)</option> <option value="2">復(fù)審?fù)ㄟ^(guò)</option> <option value="3">審核不通過(guò)</option> </select>
Python-selenium中的操作
先以python為例,查看Selenium代碼select.py文件的實(shí)現(xiàn):
...\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類(lèi)的實(shí)現(xiàn)需要一個(gè)元素的定位。并且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() 方法的使用并符合上面的給出的下拉框的要求,因?yàn)樗笙吕虻倪x項(xiàng)必須要有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>標(biāo)簽的value值。最終,可以通過(guò)下面有實(shí)現(xiàn)選擇下拉框的選項(xiàng)。
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') #初審?fù)ㄟ^(guò)
Select(sel).select_by_value('2') #復(fù)審?fù)ㄟ^(guò)
Select(sel).select_by_value('3') #審核不通過(guò)
Java-selenium中的操作
當(dāng)然,在java中的用法也類(lèi)似,唯一不區(qū)別在語(yǔ)法層面有。
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"); //初審?fù)ㄟ^(guò)
sel.selectByValue("2"); //復(fù)審?fù)ㄟ^(guò)
sel.selectByValue("3"); //審核不通過(guò)
}
}
相關(guān)文章
IntelliJ IDEA中properties文件顯示亂碼問(wèn)題的解決辦法
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中properties文件顯示亂碼問(wèn)題的解決辦法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10
SpringBoot前后端分離項(xiàng)目之打包、部署到服務(wù)器詳細(xì)圖文流程
作為后臺(tái)開(kāi)發(fā),項(xiàng)目打包部署是經(jīng)常性的操作,下面這篇文章主要給大家介紹了關(guān)于SpringBoot前后端分離項(xiàng)目之打包、部署到服務(wù)器的相關(guān)資料,文中通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
解決沒(méi)有@RunWith 和 @SpringBootTest注解或失效問(wèn)題
這篇文章主要介紹了解決沒(méi)有@RunWith 和 @SpringBootTest注解或失效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
springboot程序啟動(dòng)慢-未配置hostname的解決
這篇文章主要介紹了springboot程序啟動(dòng)慢-未配置hostname的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
這一次搞懂Spring代理創(chuàng)建及AOP鏈?zhǔn)秸{(diào)用過(guò)程操作
這篇文章主要介紹了這一次搞懂Spring代理創(chuàng)建及AOP鏈?zhǔn)秸{(diào)用過(guò)程操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
springboot整合activity自動(dòng)部署及部署文件命名流程
這篇文章主要介紹了springboot整合activity自動(dòng)部署及部署文件命名流程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Java進(jìn)階學(xué)習(xí)之如何開(kāi)啟遠(yuǎn)程調(diào)式
Java開(kāi)發(fā)中的遠(yuǎn)程調(diào)試是一項(xiàng)至關(guān)重要的技能,特別是在處理生產(chǎn)環(huán)境的問(wèn)題或者協(xié)作開(kāi)發(fā)時(shí),這篇文章主要介紹了Java進(jìn)階學(xué)習(xí)之如何開(kāi)啟遠(yuǎn)程調(diào)式的相關(guān)資料,需要的朋友可以參考下2025-03-03
SpringBoot項(xiàng)目使用mybatis-plus逆向自動(dòng)生成全套代碼
在JavaWeb工程中,每一個(gè)SSM新項(xiàng)目或者說(shuō)是SpringBoot項(xiàng)目也好,都少不了model、controller、service、dao等層次的構(gòu)建。使用mybatis-plus逆向可以自動(dòng)生成,感興趣的可以了解一下2021-09-09

