Sometimes we come across the drop -down box for <select></select> tags. Clicking the option in the drop-down box directly is not necessarily possible. Selenium specifically provides a Select class to handle the drop-down box.
<SelectID= "status"class= "Form-control valid"onchange=""name= "status"> <optionvalue=""></option> <optionvalue= "0">Not audited</option> <optionvalue= "1">First Instance through</option> <optionvalue= "2">Review by</option> <optionvalue= "3">Audit does not pass</option></Select>
operations in the Python-selenium
Take Python , for example, to see the implementation of the Selenium code select.py file:
... \selenium\webdriver\support\select.py
classSelect:def __init__(self, webelement):"""Constructor. A check is made this given element is, indeed, a SELECT tag. If It is a unexpectedtagnameexception is thrown. : Args:-webelement-element SELECT element to wrap Example:from selenium.webdriver.su Pport.ui Import select \ n Select (Driver.find_element_by_tag_name ("select")). Select_by_index (2)""" ifWebelement.tag_name.lower ()! ="Select": RaiseUnexpectedtagnameexception ("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 andMulti! ="false"
Viewing The implementation of the Select class requires the positioning of an element. and The example is given in Example.
Select (Driver.find_element_by_tag_name ("select")). Select_by_index (2)
defSelect_by_index (self, index):"""Select the option at the given index. This is do by examing the "index" attribute of a element, and not merely by counting. : Args:-index-the option at this index would be selected"""Match=STR (index) Matched=False forOptinchself.options:ifOpt.get_attribute ("Index") ==match:self._setselected (opt)if notself.is_multiple:returnmatched=Trueif notmatched:RaiseNosuchelementexception ("Could not locate element with index%d"% index)
Continue to view the use of the Select_by_index () method and conform to the requirements of the drop- down box given above, because it requires that the drop-down box option must have an index property, such as index= "1".
defSelect_by_value (self, value):"""Select all options, which has a value matching the argument. That's, when given ' foo ' this would select a option like: <option value= "foo" >BAR</OPTION&G T : 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 forOptinchopts:self._setselected (opt)if notself.is_multiple:returnmatched=Trueif notmatched:RaiseNosuchelementexception ("cannot locate option with value:%s"% value)
Continuing to view the select_by_value () method meets our requirements and is used to pick the value of the <option> tag . Finally, there are options to implement the Select drop-down box below.
fromSelenium.webdriver.support.selectImportSelect......sel= Driver.find_element_by_xpath ("//select[@id = ' status ']") Select (SEL). Select_by_value ('0')#not auditedSelect (SEL). Select_by_value ('1')#First instance throughSelect (SEL). Select_by_value ('2')#Review bySelect (SEL). Select_by_value ('3')#audit does not pass
operations in the Java-selenium
Of course, the usage in Java is similar, and the only difference is in the grammatical level.
Packagecom.jase.base;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.By.ById;ImportOrg.openqa.selenium.chrome.ChromeDriver;ImportOrg.openqa.selenium.support.ui.Select; Public classSelecttest { Public Static voidMain (string[] args) {Webdriver driver=NewChromedriver (); Driver.get ("Http://www.you_url.com"); //...Select sel=NewSelect (Driver.findelement (Byid.xpath ("//select[@id = ' status ']"))); Sel.selectbyvalue ("0");//not auditedSel.selectbyvalue ("1");//First instance throughSel.selectbyvalue ("2");//Review bySel.selectbyvalue ("3");//audit does not pass }}
Selenium processing the drop-down box for select labels