Selenium is an open source and portable automated software testing tool for testing web applications that have the ability to run on different browsers and operating systems. Selenium is really not a single tool, but a set of tools that help testers automate their applications more effectively.
Sometimes we will encounter the drop-down box of the <select></select> tag. It is not necessarily feasible to just click on the option in the drop-down box. Selenium provides a Select class specifically to handle drop-down boxes.
<select id="status" onchange="" name="status"><option value=""></option><option value="0">Not reviewed</option><option value="1">Preliminary review passed</option><option value="2">Review passed</option><option value="3">Review failed</option></select>
Operations in Python-selenium
Take python as an example to view the implementation of the Selenium code select.py file:
.../selenium/webdriver/support/select.py
class Select:
def __init__(self, wewebrem):""Constructor. A check is made that the given element is, indeed, a SELECT tag. If it is not,then an UnexpectedTagNameException is thrown.:Args:- wewebrem - 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 weweblement.tag_name.lower() != "select":raise UnexpectedTagNameException("Select only works on <select> elements, not on <%s>" %weblement.tag_name)self._el = weweblementmulti = self._el.get_attribute("multiple")self.is_multiple = multi and multi != "false" To view the implementation of the Select class, you need to locate an element. And an example sentence is given in 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 examining 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 = 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)Continue to view the use of the select_by_index() method and meet the requirements of the drop-down box given above, because it requires that the options of the drop-down box must have an index attribute, such as 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 = 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) Continue to see that the select_by_value() method meets our requirements, which is used to select the value value of the <option> tag. Finally, you can use the following option to select the drop-down box.
from selenium.webdriver.support.select import Select
...
sel = driver.find_element_by_xpath("//select[@id='status']")
Select(sel).select_by_value('0') #Not reviewed
Select(sel).select_by_value('1') #Preliminary review
Select(sel).select_by_value('2') #Review passed
Select(sel).select_by_value('3') #The review failed
Operations in Java-selenium
Of course, the usage in Java is also similar, the only difference is at the syntax level.
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"); //Not reviewed
sel.selectByValue("1"); //Preliminary review passed
sel.selectByValue("2"); //Review passed
sel.selectByValue("3"); //The review does not pass
}
}