如何找到下拉列表的选定选项?
我们如何在Selenium Webdriver中找到Dropdown的选定选项?
我试过了 –
WebElement element = driver.findElement(By.xpath(locator1)); Select select = new Select(element); List SO = select.getAllSelectedOptions(); String S = SO.toString(); System.out.println(S);
返回WebElements,如 –
[[[[[FirefoxDriver: firefox on XP (c388e8a8-09d5-41b9-b086-0278c639d8b1)] -> xpath: .//*[@id='city']]] -> tag name: option]]
我想找到被选中的选项?
您可以在Select
对象上使用getFirstSelectedOption()
方法来解析所选元素,您可以通过getText()
方法获取它的文本。 见下面的例子:
单选:
WebElement element = driver.findElement(By.xpath(locator1)); Select select = new Select(element); WebElement selectedOption = select.getFirstSelectedOption(); System.out.println(selectedOption.getText());
多种选择:
WebElement element = driver.findElement(By.xpath(locator1)); Select select = new Select(element); List selectedOptions = select.getAllSelectedOptions(); for(WebElement selectedOption : selectedOptions) { System.out.println(selectedOption.getText()); }