org.openqa.selenium.support.ui.Select类的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(8.0k)|赞(0)|评价(0)|浏览(166)

本文整理了Java中org.openqa.selenium.support.ui.Select类的一些代码示例,展示了Select类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Select类的具体详情如下:
包路径:org.openqa.selenium.support.ui.Select
类名称:Select

Select介绍

[英]Models a SELECT tag, providing helper methods to select and deselect options.
[中]为选择标记建模,提供用于选择和取消选择选项的辅助方法。

代码示例

代码示例来源:origin: spring-io/initializr

private Object getInputValue(WebElement input) {
  Object value = null;
  String type = input.getAttribute("type");
  if ("select".equals(input.getTagName())) {
    Select select = new Select(input);
    if (select.isMultiple()) {
      value = select.getAllSelectedOptions().stream().map(this::getValue)
          .collect(Collectors.toList());
    }
    else {
      value = getValue(select.getFirstSelectedOption());
    }
  }
  else if (Arrays.asList("checkbox", "radio").contains(type)) {
    if (input.isSelected()) {
      value = getValue(input);
    }
    else {
      if (Objects.equals(type, "checkbox")) {
        value = false;
      }
    }
  }
  else {
    value = getValue(input);
  }
  return value;
}

代码示例来源:origin: selenide/selenide

private void selectOptionsByTexts(WebElementSource selectField, String[] texts) {
 Select select = new Select(selectField.getWebElement());
 for (String text : texts) {
  try {
   select.selectByVisibleText(text);
  }
  catch (NoSuchElementException e) {
   throw new ElementNotFound(selectField.driver(), selectField.getSearchCriteria() + "/option[text:" + text + ']', exist, e);
  }
 }
}

代码示例来源:origin: selenide/selenide

private void selectOptionByValue(WebElementSource selectField, Select select, String value) {
  try {
   select.selectByValue(value);
  }
  catch (NoSuchElementException e) {
   throw new ElementNotFound(selectField.driver(), selectField.getSearchCriteria() + "/option[value:" + value + ']', exist, e);
  }
 }
}

代码示例来源:origin: stackoverflow.com

Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down")));
select.deselectAll();
select.selectByVisibleText("Value1");

代码示例来源:origin: selenide/selenide

private String getSelectedOptionsTexts(WebElement element) {
 List<WebElement> selectedOptions = new Select(element).getAllSelectedOptions();
 StringBuilder sb = new StringBuilder();
 for (WebElement selectedOption : selectedOptions) {
  sb.append(selectedOption.getText());
 }
 return sb.toString();
}

代码示例来源:origin: stackoverflow.com

WebElement select = driver.findElement(By.name("myselect"));
Select dropDown = new Select(select);           
String selected = dropDown.getFirstSelectedOption().getText();
if(selected.equals(valueToSelect)){
  //already selected; 
  //do stuff
}
List<WebElement> Options = dropDown.getOptions();
for(WebElement option:Options){
  if(option.getText().equals(valueToSelect)) {
   option.click(); //select option here;       
  }               
}

代码示例来源:origin: org.xwiki.platform/xwiki-platform-test-ui

/**
 * @return the selected year
 */
public String getYear()
{
  Select yearSelector = new Select(container.findElement(By.className("year")));
  return yearSelector.getFirstSelectedOption().getText();
}

代码示例来源:origin: com.epam.jdi/jdi-uitest-web

protected String getTextAction() {
  String result = "";
  if (element().getLocator().toString().contains("select")) try {
    result = new Select(element().getWebElement()).getFirstSelectedOption().getText();
  } catch (Exception ignore) {}
  return result != null && !result.equals("")
    ? result
    : element().getText();
}

代码示例来源:origin: org.xwiki.platform/xwiki-platform-test-ui

public void setFieldValue(WebElement fieldElement, String value)
{
  if ("checkbox".equals(fieldElement.getAttribute("type"))) {
    setCheckBox(fieldElement, value.equals("true"));
  } else if ("select".equals(fieldElement.getTagName())) {
    Select select = new Select(fieldElement);
    select.selectByValue(value);
  } else {
    fieldElement.clear();
    fieldElement.sendKeys(value);
  }
}

代码示例来源:origin: TEAMMATES/teammates

public void selectRecipient(int qnNumber, int responseNumber, String recipientName) {
  Select selectElement = new Select(browser.driver.findElement(
      By.name(Const.ParamsNames.FEEDBACK_RESPONSE_RECIPIENT + "-" + qnNumber + "-" + responseNumber)));
  selectElement.selectByVisibleText(recipientName);
}

代码示例来源:origin: naveenanimation20/PageObjectModel

public void createNewContact(String title, String ftName, String ltName, String comp){
  Select select = new Select(driver.findElement(By.name("title")));
  select.selectByVisibleText(title);
  
  firstName.sendKeys(ftName);
  lastName.sendKeys(ltName);
  company.sendKeys(comp);
  saveBtn.click();
  
}

代码示例来源:origin: sayems/java.webdriver

public void selectByText(Supplier<By> by, String select) {
  final Element element = untilFound(by);
  Select dropdown = new Select(element);
  String textOption = dropdown
      .getOptions()
      .parallelStream().filter(opt -> opt.getText()
          .contains(select))
      .findFirst()
      .get().getText();
  dropdown.selectByVisibleText(textOption);
}

代码示例来源:origin: tarun3kumar/seleniumtestsframework

public void init() {
  super.init();
  try {
    select = getNewSelectElement(element);
    options = select.getOptions();
  } catch (UnexpectedTagNameException e) {
    if (element.getTagName().equalsIgnoreCase("ul")) {
      options = element.findElements(By.tagName("li"));
    }
  }
}

代码示例来源:origin: TEAMMATES/teammates

private String getGenerateOptionsForMsqValue(int qnNumber) {
  Select options = new Select(browser.driver.findElement(By.id("msqGenerateForSelect-" + qnNumber)));
  return options.getFirstSelectedOption().getText();
}

代码示例来源:origin: selenide/selenide

@Override
public Void execute(SelenideElement proxy, WebElementSource selectField, Object[] args) {
 String text = (String) args[0];
 WebElement element = selectField.getWebElement();
 Select select = new Select(element);
 List<WebElement> options = element.findElements(By.xpath(
   ".//option[contains(normalize-space(.), " + Quotes.escape(text) + ")]"));
 if (options.isEmpty()) {
  throw new NoSuchElementException("Cannot locate option containing text: " + text);
 }
 for (WebElement option : options) {
  setSelected(option);
  if (!select.isMultiple()) {
   break;
  }
 }
 return null;
}

代码示例来源:origin: com.github.jjYBdx4IL/github-test-utils

@Test
public void testSetInputFieldValue_String_String() throws Exception {
  getDriver().get(testPage1.toExternalForm());
  setInputFieldValue("inputName1", "valueOfInputName1");
  WebElement el = getDriver().findElement(By.xpath("//input[@name='inputName1']"));
  assertEquals("valueOfInputName1", el.getAttribute("value"));
  setInputFieldValue("selectName", "GenericVal");
  el = getDriver().findElement(By.xpath("//select[@name='selectName']"));
  assertEquals("GenericVal", new Select(el).getFirstSelectedOption().getAttribute("value"));
}

代码示例来源:origin: stackoverflow.com

Select select = new Select(driver.findElement(
          By.id("ctl00_main_ddlAssocAccountNumbers")));
List<WebElement> listOfOptions = select.getOptions();
for (WebElement option : listOfOptions) {
  String storeAndAcctNum = option.getText();
  if (storeAndAcctNum.contains(accountNum)) { // if sub-string matches
    select.selectByVisibleText(storeAndAcctNum); // fire select as usual
    break; // out of the loop
  }
}

代码示例来源:origin: com.epam.jdi/jdi-light

public Select select() {
  WebElement select = get();
  if (!select.getTagName().equals("select")) {
    List<WebElement> els = select.findElements(By.tagName("select"));
    if (els.size() > 0)
    select = els.get(0);
  }
  return new Select(select);
}
@JDIAction(level = DEBUG)

代码示例来源:origin: net.serenity-bdd/serenity-core

public Set<String> getSelectedOptionLabels() {
  List<WebElement> options = new Select(dropdownField).getAllSelectedOptions();
  Set<String> selectedOptions = new HashSet<>();
  for (WebElement option : options) {
    if (option.isSelected()) {
      selectedOptions.add(option.getText());
    }
  }
  return selectedOptions;
}

代码示例来源:origin: TEAMMATES/teammates

/**
 * Selects the option by visible text and returns whether the dropdown value has changed.
 *
 * @throws AssertionError if the selected option is not the one we wanted to select
 *
 * @see Select#selectByVisibleText(String)
 */
boolean selectDropdownByVisibleValue(WebElement element, String text) {
  Select select = new Select(element);
  WebElement originalSelectedOption = select.getFirstSelectedOption();
  select.selectByVisibleText(text);
  WebElement newSelectedOption = select.getFirstSelectedOption();
  assertEquals(text, newSelectedOption.getText().trim());
  return !newSelectedOption.equals(originalSelectedOption);
}

相关文章